The Rio Olympics ended with a spectacular carnival-inspired closing ceremony, and the official handover to 2020 hosts Tokyo.
The colourful ceremony, lasting almost three hours, celebrated Brazil's arts and was held in a wet Maracana.
Among the highlights were Tokyo's impressive showcase and a vibrant carnival parade.
"These were a marvellous Olympics, in a marvellous city," said International Olympic Committee chief Thomas Bach.
"Over the last 16 days a united Brazil inspired the world, in difficult times for all of us, with its irresistible joy for life."
Bach officially closed the Games of the 31st Olympiad after 16 days of competition, featuring 11,303 athletes from 206 nations and a refugee team.
One of the biggest cheers of the night came when Japan's Prime Minister Shinzo Abe appeared dressed as computer game character Super Mario, rising out of a huge green pipe in the stadium.
The ceremony, watched by billions around the world, featured the parade of athletes and a dramatic extinguishing of the Olympic flame.
Women's hockey captain Kate Richardson-Walsh carried the flag for Great Britain, who finished the Games with 67 medals - their highest tally at an overseas Olympics.
Super-heavyweight boxer Joe Joyce had earlier won Britain's final medal of Rio 2016 - a silver - as they finished second in the medal table to the United States, ahead of China.
- Relive the closing ceremony
- How did Team GB make history?
- A review of Team GB's record haul
- Rio 2016's best moments in pictures
- The Maracana was turned into a street carnival as the ceremony celebrated Brazil's art, music and dance.
- Highlights included human formations of iconic Rio landmarks Christ the Redeemer and Sugarloaf Mountain.
- The Olympic flame was extinguished by a rain shower, which then gave life to a large tree sculpture to symbolise rebirth.
- The Rio carnival anthem 'Cidade Maravilhosa' played as the party reached a crescendo.
- Brazilian model Izabel Goulart led a parade of 50 women and 200 dancers, who were joined by a sound truck containing 12 carnival queens.
- The ceremony concluded with a confetti and firework show.
The 'Heroes of the Games' parade
Tokyo's time comes
The ceremony featured the symbolic handover of the Olympic flag. Rio mayor Eduardo Paes returned it to IOC president Bach, before it was passed to Tokyo governor Yuriko Koike.
That was followed by an impressive 12-minute showcase of Tokyo, which included Prime Minister Abe's star turn.
Who said what?
IOC president Thomas Bach: "Brazil we love you, thank you for your warm hospitality. Over the last 16 days a united Brazil inspired the world, in difficult times for all of us, with its irresistible joy of life. You gave many reasons to be proud. These Games demonstrated that diversity is an enrichment for everyone. These Games were a celebration of diversity.
"These were a marvellous Olympic Games in a marvellous city. They are leaving a unique legacy for many generations to come. History will talk about a Rio before and a much better Rio after these Games."
Rio 2016 organising committee president Carlos Arthur Nuzman: "The Games in Rio is a great challenge, but a challenge with success. I am proud of my country, my city and my people. Rio has delivered history."
Historic Games for Team GB
From swimmer Adam Peaty's gold on day three to Mo Farah's long-distance 'double double' on the final Saturday, this was Britain's most successful Games in 108 years.
In the velodrome, Team GB won more than double the amount of medals of their nearest challengers.
Jason Kenny and Laura Trott will marry in September as the owners of 10 gold medals, while Sir Bradley Wiggins became the most decorated British Olympian with his eighth medal.
Then there was Farah, who won his fourth Olympic gold and became only the second man to retain the 5,000m and 10,000m titles.
Dressage rider Charlotte Dujardin won her third gold medal, while Justin Rose won his first as golf returned to the Olympics.
There were also historic golds in gymnastics, women's hockey and diving, while 58-year-old show jumper Nick Skelton became the country's oldest medallist for a century.
Gymnast Max Whitlock won two golds, while boxer Nicola Adams, taekwondo's Jade Jones, triathlete Alistair Brownlee and tennis player Andy Murray were among those who became multiple gold medallists.
The end of an era
Jamaican sprinter Usain Bolt said goodbye to the Olympics by winning an unprecedented 'triple triple'.
The 30-year-old Jamaican won his seventh, eighth and ninth gold medals by claiming the 100m, 200m and 4x100m relay titles for the third Games in succession.
US swimmer Michael Phelps, meanwhile, took his medal tally to 28 from five Olympics by winning six in Rio - five of which were golds.
The 31-year-old's efforts helped the US top the medal table with 121, their most successful 'away' Games.
Next in line...
South Africa's Wayde van Niekerk, 24, delivered one of the greatest performances of the Games as he broke Michael Johnson's 17-year-old 400m record.
In doing so he became one of the faces of Rio 2016 - as did US gymnast Simone Biles, the 19-year-old winning four gold medals.
US swimming looks in good health if Phelps retires, with the emergence of Katie Ledecky. Another 19-year-old, she won four golds and a silver in the pool.
A Games of firsts
The Rio Games brought first medals for Fiji, Jordan and Kosovo - and all three opened their accounts with gold, Fiji in the first Olympic rugby sevens competition.
Competitors from Bahrain, Puerto Rico, Singapore, Vietnam and Tajikistan, too, won their first Olympic titles.
Twenty-seven world records were set in Rio within the seven Olympic sports that recognise them - archery, athletics, modern pentathlon, track cycling, shooting, swimming and weightlifting.
The home nation's redemption
Brazil's Olympics took off when Rio native Rafaela Silva, 24, won judo gold on day three.
And they finished on a high as the men's volleyball team beat Italy for gold on the closing day of the Games.
The hosts finished 13th in the medal table with 19 medals, seven of which were gold.
None got a bigger reception than when Neymar scored the winning penalty in the shootout as Brazil beat Germany to win their first men's Olympic football gold medal.
It came in front of 78,000 fans in the Maracana, two years after Germany thrashed hosts Brazil 7-1 in the 2014 World Cup semi-finals.
The build-up to the Games was dominated by a Russian doping scandal, fears over the Zika virus and issues with the city's security, infrastructure and venues.
The Games were not affected by any major incidents, but they did not pass without some hitches:
- The diving pool turned green.
- There were scores of empty seats at venues.
- Controversial decisions marred the boxing tournament.
- There were security problems, including a bullet shot into the media tent at the equestrian venue and a media bus being attacked.
- The layout of the cycling road race course was criticised.
- Russian swimmer Yuliya Efimova, who had been banned for doping, was repeatedly booed.
But perhaps the biggest embarrassment was felt by US swimmer Ryan Lochte.
The gold medallist apologised to the people of Brazil after "over-exaggerating" claims he and three team-mates were robbed at gunpoint in Rio.
The IOC announced in June that a group of 10 refugee athletes would compete in an Olympic Games for the first time.
The team of swimmers, judokas and runners competed under the flag of the IOC.
"We are equal now," said marathon runner Yonas Kinde, who is originally from Ethiopia. "We compete like human beings, like the others."
Elsewhere, USA's Abbey D'Agostino and New Zealand's Nikki Hamblin were given fair play awards after stopping to help each other during their 5,000m semi-final.
And there were several proposals as the love bug bit the Rio Olympics.
One of the hottest tickets in town seemed to be for Dan Walker's BBC Four's Olympic coverage from Copacabana beach.
Highlights included a Brazilian hen party popping in, which ended with a wedding invitation for the BBC presenter, and the Copacabinmen.
Subscribe to the BBC Sport newsletter to get our pick of news, features and video sent to your inbox.