The USA ran out 107-100 winners - the closest final since the Soviet Union beat the USA 51-50 40 years ago.
Kevin Durant scored 30 points to lead the champions, while LeBron James had 19 and Kobe Bryant 17.
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 USA, had two threes of his own as they came back to lead 15-12, and his team 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 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, and Sergio Llull eventually 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 USA responded with six straight points to go 50-44 up on Kevin Love's throws. Deron Williams then made it 58-51 with a three-pointer.
But 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 USA 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 USA 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 USA were up 102-93.
Paul's driving lay-up settled it 50 seconds from time as the gap grew to 11, and USA coach Mike Krzyzewski responded with a mass of substitutions as James and Bryant began the celebrations on the bench.
Earlier in the day, Russia secured bronze with an 81-77 win over Argentina.