Neymar sent Brazil into ecstasy as he clinched Olympic football gold and Mo Farah sealed a rare distance double-double in a pulsating final evening of track action in Rio on Saturday.
Neymar swept home a brilliant free-kick and scored a trademark stutter-run penalty to win a shoot-out against Germany as the hosts headed towards Sunday's closing ceremony on a high.
Later, Britain's Farah headlined the last night of track and field as he won a thrilling 5,000m race to earn the first consecutive 5,000m-10,000m doubles in 40 years.
Matt Centrowitz produced the biggest upset when he beat defending champion Taoufik Makhloufi of Algeria to become America's first 1,500m champion in more than a century.
And the United States ran away with both the men's and women's 4x400m relays as the curtain closed on the last full day of competition, with only 12 gold medals available on Sunday.
With 294 of 306 titles decided, the United States led the medals table with 43 golds, with Britain in surprise second place with 27 to China's 26.
More From This Section
China won their third Olympic Games women's volleyball gold with a 3-1 victory over Serbia. Only the former Soviet Union — with four golds — has been more successful in Olympics women's volleyball than China.
South Korea's Park In-Bee won the first women's golf Olympic gold in 116 years by a commanding five strokes, and the USA routed Spain in the women's basketball final.
Millions of Brazilians were glued to the men's football and celebrations were long and loud when the five-time world champions grabbed their first Olympic title at the Maracana stadium.
Victory, 5-4 on penalties after the scores were locked at 1-1 after extra time, left Neymar and his team-mates sobbing uncontrollably as they erased the nightmare of Brazil's 7-1 World Cup semi-final defeat to Germany two years ago.
It was a fairytale finish for Neymar, who missed the 2014 defeat with a back injury and faced heavy criticism for his performances early in the Olympic competition.
"This is one of the best things that has happened in my life," said the Barcelona star who promptly stepped down as captain.
"Now (the critics will) have to swallow what they said."
Victory may not get Brazil into the top 10 of the Olympic medals table but it was the one gold the country wanted. Street celebrations erupted after the final whistle.
Security scares, transport problems and public apathy are among the problems that have bedevilled South America's first Games, but Olympics chief Thomas Bach hailed the event as "iconic".
"The Brazilians altogether were great hosts and really united behind these Games... They turned this great competition into a party for everybody," said Bach.
Farah was pushed hard in the 5,000m before he kicked home in the final straight to become the first man since Finnish great Lasse Viren in 1976 to retain two Olympic distance titles.
"This is the most satisfying win of the four, it is incredible," said the 33-year-old Somali-born Londoner.
In the 1,500m men's race, Centrowitz became the first American man since the 1908 London Games to win the event, which has been shared exclusively between African runners since 1996.
US track golden girl Allyson Felix led home her team in the women's 4x400m, crossing the line to claim a record sixth gold medal.
In the men's 4x400m, former Olympic 400m champion LaShwan Merritt led the US team home with Jamaica finishing in silver and defending champion the Bahamas bronze.
South African runner Caster Semenya completed a convincing victory in the 800m to win her first Olympic gold, four years after being denied gold by Russian dope cheat Mariya Savinova.
Semenya has been at the centre of a raging controversy because of her naturally occurring condition, hyperandrogenism, which causes elevated testosterone levels.
Rival runners want authorities to revisit rules limiting the amount of testosterone allowed in competitors.
Park triumphed in the first women's Olympic golf tournament since 1900 by five strokes from New Zealand's Lydia Ko, underlining the dominance of South Korea's superlative women's golfers.
"It felt like this week was so long and mentally and physically exhausting," said Park.
"But I'm happy with the result and when you play for your country there is some extra power it gives you and I think that drove me on the golf course."
Russia came to Rio under a doping scandal shadow and missing its key track and field stars.
But they snared four golds on Saturday — in women's handball, team rhythmic gymnastics, modern pentathlon and freestyle wrestling — to reach 17, fourth on the medals table.
Earlier, China's Chen Long inflicted more Olympic heartache on Lee Chong Wei with a convincing win in the men's badminton singles final — consigning the Malaysian to his third straight silver medal.
And Britain's Nicola Adams became the first woman to retain an Olympic boxing title in the flyweight division.
"I'm now the most accomplished British amateur boxer we've ever had, of all time, so it's a nice title to have," said Adams, 33.