The United States overcame some fierce resistance from Spain 107-100 to land their fifth basketball gold in the six Olympic Games since the start of the 'Dream Team' era.
It was the closest final since the Soviet Union beat the Americans 51-50 40 years ago, and the second closest in Olympic history.
Kevin Durant scored 30 points to lead the Americans while LeBron James had 19 and Kobe Bryant 17.
If a crowd that included Arnold Schwarzenegger, Boris Johnson, David Beckham and Henry Kissinger - as well as members of the Spanish royal family - expected the United States to put on a clinic against a Spain team who have not always fired in London, they had another thing coming.
Spain, silver medallists in Beijing in another close final, started strongly as Juan Carlos Navarro, who has been suffering with a foot problem, found his form and hit a pair of three-pointers to fire them into a 12-7 lead.
Bryant, playing well in spurts for the Americans, had two threes of his own as they came back to lead 15-12.
The Americans briefly threatened to run away late in the first quarter with a three from Carmelo Anthony making it 25-16.
Durant's three from the corner made it 35-25 late in the first, but Spain sent a message with Sergio Rodriguez's alley-oop pass slammed home by Rudy Fernandez.
The European champions scored the first seven points of the second quarter to slice the gap to 35-34 on Rodriguez's free throws.
The Americans were benefiting from some questionable calls as the referees made their presence felt but they still could not pull away and Sergio Llull made it 42-41 to the Spanish.
Spain suffered a huge blow when Marc Gasol, surprisingly left in the game by coach Sergio Scariolo when on three fouls, picked up his fourth to take a seat midway through the second quarter.
The United States responded with six straight points to go 50-44 up on Kevin Love's three throws.
Deron Williams made it 58-51 with a three-pointer but some of the many foul calls were starting to go against the Americans.
When Andre Iguodala was called for an unsportsmanlike foul, Fernandez made it 59-58 before the break.
Pau Gasol scored the first 13 Spanish points of the third quarter, on a personal mission to keep his team in it, and they were still down only one, 83-82, at the start of the fourth.
Chris Paul hit a three and then drove to the hoop for an easy basket as the Americans eased into a 90-84 lead.
James then picked up his fourth foul with an accidental sweep of Pau Gasol, who headed to the bench as his brother returned to cut the gap to four.
Paul led the Americans clear once more with five quick points and Bryant's short jump shot made it 97-87.
Navarro and Fernandez scored to briefly rally the Spanish, but when James nailed a three the Americans were up 102-93.
Paul's driving lay-up settled it 50 seconds from time as the gap grew to 11, and American coach Mike Krzyzewski responded with a mass of substitutions as James and Bryant began the celebrations on the bench.
Paul finished with 11 points for the Americans. Pau Gasol had 24 points for Spain and Navarro 21.
Marc Gasol had 17 points in only 17 minutes of action, while Fernandez added 14 and Serge Ibaka 12.