The Rio Olympic closing ceremony took place in a blustery storm, a touch of melancholy and a sense of pride on Sunday as Brazil breathed a collective sigh of relief at having pulled off South America's first Games.
Brazil, which battled with empty seats, security scares and a mysterious green diving pool, ended the games with two late gold medals for the host country in its two favorite sports, men's soccer and volleyball. The wins helped smooth some of the rough edges around the Games for Brazilians.
From the Maracana where it all began 16 days ago, the final event kicked off with figures dressed as multi-colored macaws flying over Rio's world-famous landmarks, Christ the Redeemer and Sugarloaf Mountain, before forming the five Olympic rings.
A storm that menaced Rio all day sent wind and rain through Brazil's most storied stadium and the power briefly went out in part of the stadium and the surrounding neighborhood shortly before the ceremony kicked off.
Rain drenched performers and hundreds of athletes as they entered the party, many with medals hanging around their necks.
To the beat of traditional Brazilian music, Olympians danced and waved their countries' flags to celebrate their place on the world's premier sporting stage. The first Refugee team in Olympic history, one of the biggest crowd-pleasers of the Games, marched in behind the Olympic flag, carried by a Congolese judoka and Rio resident.
In the last of 306 medal ceremonies, International Olympic Committee President Thomas Bach draped the gold around the neck of Kenya's Eliud Kipchoge, 31, who 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 also claimed the final gold of the Games with a crushing 96-66 win over Serbia.
Bach declared the Rio Games closed and expressed hope that they had left a lasting mark on the metropolitan area of 12 million people.
"These Olympic Games are leaving a unique legacy for generations to come," he said. "History will talk about a Rio de Janeiro before and a much better Rio de Janeiro after the Olympic Games."
In a final symbolic act, the Olympic flame that had burned since August 5 was then extinguished in a downpour of artificial rain. The city handed over the Olympic flag to Tokyo, site of the 2020 Summer Games.
Prime Minister Shinzo Abe, meeting Japanese athletes in Rio earlier on Sunday, said Tokyo now wanted to put on the best Olympics yet.
"The Japanese government will strive hard so you can participate in the Tokyo 2020 Olympics so you will be in your best condition, and work hard so it will be the best Olympics ever," Abe told the Japanese athletes.
The good and the bad
Rio will be remembered for the comeback of American swimmer Michael Phelps, who won five golds and one silver to reinforce his distinction as the most decorated Olympian of all time.
Jamaica's Usain Bolt drew down the curtain on his brilliant Olympic career by securing a sweep of the sprint titles for a third successive Games.
And American gymnast Simone Biles, the US flag bearer in the closing ceremony, kicked off her Olympic run by tying the record of four gold medals in a single Games.
At times, it was hard to focus on the great sporting moments happening across the sprawling city.
A low point for Rio came when Ryan Lochte, one of America's most decorated swimmers, said he was robbed at gunpoint. That ignited further security concerns after a series of assaults against government ministers, athletes and tourists.
But Lochte's story quickly unravelled when police discovered he fabricated his story to cover up his vandalizing a gas station after a drunken night out with three teammates. His lie enraged Brazilians and Americans alike.
Brazilians could take heart in the fact that there were no major mishaps or breaches after deadly attacks in Europe and the United States had prompted the biggest security operation in Brazil's history. The military and police presence was extraordinary with 85,000 troops fanned out across venues, streets and transport hubs, double the deployment in London four years ago.