The last American gold of the Olympics did not come easily, as a U.S. basketball team that included Lebron James and Kobe Bryant at one point were behind Spain on the final afternoon of Olympic sport.
The U.S. basketball gold was the Americans' last of 46, for a total of 104. That put them well ahead of No. 2 China, which won 87 medals, including 38 golds. Britain won 29 golds, third-most of any nation, and 65 medals overall — good for fourth in that category behind Russia, a winner of 82 medals, 24 of them gold.
The games ended with a huge party in the main stadium that began with a shout-out to Winston Churchill and a celebration of the Union Jack. It including rock 'n' roll rickshaws, dustbin percussionists, an exploding yellow car and a marching band in red tunics.
The show, put together by artistic director Kim Gavin, also featured a parade of the 10,800 athletes, who marched in as one, rather than with their nations, symbolizing the harmony and friendship inspired by the games. The crowd cheered and the Olympians gave it right back, waving flags and arms.
Later in the ceremony, London to organizers handed the Olympic flag to organizers of the 2016 Games in Rio de Janeiro.
"We will never forget the smiles, the kindness and the support of the wonderful volunteers, the much-needed heroes of these games. You, the spectators and the public, provided the soundtrack for these games," IOC President Jacques Rogge said, as he declared the games closed.
Earlier on the basketball court, The U.S. was ahead just 97-91 when James dunked and then hit a three-pointer to allow the Americans to pull away for a 107-100 win in a replay of the 2008 final at the Beijing Games. Bryant scored 17 points.
In the final minute, U.S. coach Mike Krzyzewski, in his final international game, took out his top stars who came to London determined to keep American basketball on top. When the final horn sounded, Krzyzewski locked James in a tight embrace as Bruce Springsteen's "Born In The USA" rocked the arena.
"We knew it wasn't going to be easy. We didn't want it easy," James said. "A lot of teams have won gold easy. We didn't want it that way. We're a competitive team and we love when it gets tight. That's when our will and determination kind of shows."
In the bronze final, Alexei Shved scored 25 points — 13 in the fourth quarter — and Russia won its first Olympic medal in basketball, 81-77 over Argentina.
Earlier, in the first of 15 gold medals presented on the final day of the games, it was the marathoners who got to see London at its best. Under sunny skies for the fifth day in a row, the runners left from The Mall near Buckingham Palace and took a route along the River Thames past the Tower of London and circled close to Big Ben.
At the end of their 42-kilometre tourist jaunt, it was Stephen Kiprotich of Uganda who crossed the finish line first in a time of two hours eight minutes one second. Abel Kirui of Kenya was second, 26 seconds behind, while another Kenyan, Wilson Kipsang took the bronze.
"People didn't expect Uganda. They thought Kenya, Ethiopia," Kiprotich said. "Being unknown, now I'm known."
The Kenyan team was running in memory of their countryman Sammy Wanjiru, who four years ago in Beijing captured the country's first Olympic marathon gold. But he died last year after a fall from a second-floor balcony during a domestic dispute.
"That is very challenging for us," Kenyan runner Emmanual Mutai had said ahead of the race and the difficulty of trying to win for Wanjiru.
At Hadleigh east of London, world champion Jaroslav Kulhavy of Czech Republic won a two-man sprint to claim the men's mountain bike gold medal. Kulhavy made the most of a final steep ascent on the technical circuit in the English countryside to move ahead of Nino Schurter of Switzerland and then sprinted to the line.
Schurter claimed the silver medal and Italian Marco Aurelio Fontana of Italy took bronze.
In men's boxing finals, flyweight Robeisy Ramirez won Cuba's second boxing gold medal of the games, capping a stellar run through the tournament with a 17-14 victory over Mongolia's Tugstsogt Nyambayar. Vasyl Lomachenko of Ukraine won his second consecutive gold, completing his domination of the London lightweights with a 19-9 victory over South Korea's Han Soon-chul.
Serik Sapiyev of Kazakhstan won gold in the welterweight division, overcoming Britain's Freddie Evans 17-9, while Egor Mekhontsev of Russia won the light heavyweight title on a countback tiebreaker over Kazakhstan's Adilbek. The fighters finished level at 15 points apiece, and the tiebreaker — which evaluates larger parts of the judges' total scores — also was level. The five judges then voted for their choice, and Mekhontsev claimed Russia's only boxing gold in London.
British boxer Anthony Joshua won the super heavyweight gold in similar fashion, rallying from a third-round deficit to beat defending champion Roberto Cammarelle of Italy in another tiebreaker.
It was Britain's 29th gold medal of the games, leaving the hosts third behind the leading U.S. total of 46 and China's 38.
Elsewhere, Croatia won its first Olympic gold in men's water polo, getting two second-half goals each from Miho Boskovic and Maro Jokovic to pull away from Italy for an 8-6 win. France beat Sweden 22-21 on to win its second consecutive gold medal in men's handball.
Russia won its fourth consecutive gold medal in rhythmic gymnastics group all-around while its male volleyballers came from two sets down — saving two championship points — to beat Brazil in five sets.
In the 302nd of 302 medals decided early Sunday evening, Laura Asadauskaite of Lithuania used a strong running performance in the final event to win the women's modern pentathlon.
At his final media briefing of the games, IOC President Jacques Rogge said he wanted to set the record straight: Usain Bolt was an "active" legend and the best sprinter ever.
Rogge surprised some earlier this week when he said the Jamaican runner needed to prove his greatness over more than two Olympics before achieving his self-proclaimed status of "living legend."
On Sunday, Rogge relented a bit and came up with a different wording for the six-time gold medallist .
"I mean this is purely a semantic issue," he said. "Let me finalize this issue as follows: to say that Usain Bolt is an active performance legend, he is an icon, he is the best sprinter of all time."
Bolt won the 100 and 200 metres at the London Olympics, becoming the first athlete to sweep both events at consecutive games, and anchored the Jamaican team to a world record in the 4x100-metre relay on Saturday night.
AP sports writer Dennis Passa contributed to this story.