The Rio Olympics ended in a blaze of colour with an exuberant closing ceremony on Sunday as Tokyo took up the baton promising to host the best Games yet in 2020.
After a rollercoaster fortnight which mixed off-field hitches with compelling sporting drama, Olympics chief Thomas Bach hailed a "marvellous" Games in the Brazilian city.
"These were marvellous Olympic Games in the marvellous city!" said Bach, as he declared the Games closed and the Olympic flame was extinguished.
"These Olympic Games are leaving a unique legacy for generations to come. History will talk about a Rio de Janeiro before and a much better Rio de Janeiro after the Olympic Games."
Just a few hours earlier, the United States basketball squad won the last gold of the Games, boosting their country's place on top of the medals table.
At the closing ceremony, thousands of fans and athletes donned ponchos on a wet and windy night for a colourful festival of Brazilian culture and music with bursts of spectacular fireworks.
Smiling and waving athletes danced into the Maracana stadium taking selfies as Rio's 16-day Summer Games closed ahead of the 2020 Olympics in Tokyo.
Japan's Prime Minister Shinzo Abe made a comical cameo as Nintendo video game character Super Mario as Tokyo set out its store for 2020.
"The Japanese government will... work hard so it will be the best Olympics ever," Abe said as he met Japanese medal-winners earlier.
Security scares and logistical problems were a feature of South America's first Olympics, held against the backdrop of Brazil's political and economic crisis.
Swathes of empty seats caused disquiet but the Games also witnessed the last hurrahs of both Usain Bolt and Michael Phelps, who both lived up to their billing.
Bolt sealed the sprint 'triple triple' in his final Games, reaching a record-equalling nine gold medals with his third consecutive 100m, 200m and 4x100m sweep.
Swimming legend Phelps took his unmatched career haul to 23 gold medals with another five in Rio — plus a silver, after his loss to former childhood fan Joseph Schooling.
Brazil came late to the party but the country celebrated long and loud when Neymar won a gold-medal penalty shoot-out against Germany to erase memories of their 7-1 World Cup semi-final humiliation in 2014.
On a sodden final day, Kenya's Eliud Kipchoge, 31, shone with his seventh win in eight marathons, timing 2hr 8min 44sec to win by more than a minute.
The USA's basketball 'Dream Team' of NBA stars claimed the 306th and final gold of the Games with a crushing 96-66 win over Serbia.
With the basketball win, America matched its tally of 46 golds from London four years ago to lead the medals table from Britain, who sealed surprise second place ahead of China with 27 golds to 26.
"We are one of those sporting superpowers now," beamed UK Sport chief executive Liz Nicholl.
But there was also controversy as Mongolia's Chagnaadorj Usukhbayar became the sixth weightlifter in Rio to fail a drugs test.
And police seized passports, phones and computers in a raid on the Irish Olympic office, following the arrest of Irish International Olympic Committee member Patrick Hickey over an alleged black market tickets scam.
Brazil added a final flourish as they beat Italy 3-0 to win the men's title in volleyball, one of the country's most popular sports.
French fighter Tony Yoka won a split-decision against Britain's Joe Joyce to take the super-heavyweight title in boxing.
Switzerland's world champion Nino Schurter, third in Beijing and second in London, competed his ascent of the podium by winning the men's mountain bike competition.
And there were furious scenes when a Mongolian coach stripped down to his underwear to protest a decision against wrestler Ganzorig Mandakhnaran.
Whit inputs from AFP