A blustery storm, a touch of melancholy and a sense of pride converged at the closing ceremony of the 2016 Olympics on Sunday as Brazil breathed a collective sigh of relief at having pulled off South America's first Games.

It was far from a perfect execution by Brazil, which battled with empty seats, security scares and a mysterious green diving pool. But two late gold medals for the host country in its two favourite sports, men's football and volleyball, 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-coloured 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 neighbourhood 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, like the U.S. men's basketball team which won gold on Sunday.

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.

The athletes will witness the last of 306 medal ceremonies, for the men's marathon earlier in the day.

The city will then hand over the Olympic flag to Tokyo, site of the 2020 Summer Games, and extinguish the Olympic flame, burning since Aug. 5, in a small, environmentally friendly cauldron.

Fireworks explode at the start of the closing ceremony of the Rio 2016 Olympic Games at the Maracana stadium in Rio de Janeiro on August 21, 2016. / AFP PHOTO / Yasuyoshi Chiba

SPORT COMPETES WITH SCANDAL

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 U.S. 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 vandalising 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.

'DIFFICULT MOMENT'

"Even with all our problems we pulled off a good Olympics. Nothing too bad happened and I'd say it was better than expected," said Nivea Araujo, a Rio resident attending the closing ceremony.

For many in the soccer-mad nation, the best Olympic moments happened in the Maracana, where Brazil defeated Germany in football on Saturday and pieced together a widely hailed opening ceremony despite a tight budget.

"We are in a difficult moment as a country right now, we can't hide that, but the Games were scheduled and I'm glad we could enjoy them," said Alessandro Freitas, also from Rio.

One of the major concerns for Brazilians is what will be the final cost of the Games for a country in the midst of its deepest economic recession since the 1930s and how much they actually helped improve the city's infrastructure.

And come Monday, with the Games no longer a distraction, Brazil gets back to its dour reality of duelling political and economic crises. An impeachment vote in coming days could lead to the permanent ouster of suspended President Dilma Rousseff.

Interim President Michel Temer, who was booed at the opening ceremony, decided not to attend the closing event. He said he would come for next month's Paralympic Games.