Rio 2016 Olympic Games close with spectacular carnival-themed ceremony (PHOTOS, VIDEO)
The three-hour ceremony, watched by billions around the world, featured the parade of athletes, a celebration of Brazil's arts and an amusing handover to Tokyo for the next event in four years.
International Olympic Committee President Thomas Bach officially closed the 31st Games which featured 11,303 athletes from 206 nations and a refugee team.
“Brazil we love you, thank you for your warm hospitality,” said Bach.
“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 marvelous Olympic Games in a marvelous 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,” he said.
The stadium was transformed into a street carnival as the ceremony showcased Brazil's art, music and dance.
Human formations of iconic Rio landmarks Christ the Redeemer and Sugarloaf Mountain were amongst the highlights, while the Rio carnival anthem 'Cidade Maravilhosa' played as the party reached a crescendo.
Brazilian model Izabel Goulart led a parade of 200 dancers, who were joined by a truck containing 12 carnival queens.
The Olympic flame was extinguished by a rain shower, which then gave life to a large tree sculpture to symbolize rebirth.
Rio Mayor Eduardo Paes returned the Olympic flag to Bach before it was passed to Tokyo Governor Yuriko Koike.
An impressive 12-minute showcase of Tokyo followed, featuring Japan's Prime Minister Shinzo Abe dressed as computer game character Super Mario, rising out of a huge green pipe in the stadium.
The ceremony closed with a firework and confetti show.
The build-up to the Games was marred by the Russian doping scandal, fears over the Zika virus and issues with Rio's infrastructure, security and the venues.
During the Games, organizers had to contend with numerous other problems including a bullet shot into the media tent at the equestrian venue and a media bus being attacked.
The diving pool also turned green, there were thousands of empty seats at venues and the boxing tournament was marred by controversial judging decisions.
Arguably the biggest embarrassment was US swimmer Ryan Lochte's false claims that he and three teammates were robbed at gunpoint in Rio.
The refugee team of swimmers, judokas and runners were warmly welcomed into Rio, but the 77,000 Brazilian residents displaced as a result of the Games would no doubt have felt less accommodating to the visitors.
Jamaican sprinter Usain Bolt said farewell to the Olympics by winning an unprecedented sprint 'triple triple' in the 100m, 200m and 4x100m relay, adding to his golds from Beijing 2008 and London 2012.
Star US swimmer Michael Phelps took his medal tally to 28 from five Olympics by winning six in Rio - five of which were gold.
Fiji, Jordan and Kosovo won their first ever Olympic medals - and all three opened their accounts with gold, while athletes from Singapore, Bahrain, Vietnam, Puerto Rico, Vietnam and Tajikistan also claimed their first Olympic titles.
Brazil's Olympics sparked into life when Rafaela Silva won judo gold on Day Three, but the biggest cheer was reserved for the men's football team, with Neymar scoring the winning penalty in the shootout as Brazil beat Germany to win their first-ever gold medal.
The US topped the medal charts with 121, with China (70) and Great Britain (67) completing the top three.
Despite the ban on its track and field athletes, Russia finished fourth with 56 medals, which included 19 golds.