After overwhelming everyone for seven games, the Americans led by only four points with under 2 1/2 minutes to play. Then the U.S. proved it could handle a close game that seemed would never come in Beijing.
Their prize: the first U.S. gold medal since the 2000 Olympics.
"Much respect to Spain, but the U.S. is back on top again," LeBron James said.
Argentina won the bronze with an 87-75 victory against Lithuania.
Dwyane Wade scored 27 points for the Americans, who found a much gamer Spanish team than the one it humiliated by 37 points earlier in the tournament. Kobe Bryant added 20 points.
In a game so devoid of defense that it felt more like an NBA All-Star game than one with a title at stake, the Americans had too much offense down the stretch. Bryant converted a clutch four-point play with 3:10 remaining, holding his finger to his lips to quiet the rowdy Spanish crowd behind the basket.
Wade added another 3-pointer that made it 111-104 with just over 2 minutes left, and only then could the Americans relax a little.
They began to celebrate during a break after some technical fouls on Spain with 26 seconds left, then partied at midcourt when it was over with "Born in the USA" blaring over the arena's speakers.
"We played with great character in one of the great games in international basketball history, I think," U.S. coach Mike Krzyzewski said.
Nobody else had been close to the Americans in Beijing. This team's only Olympic competition had been history, in a Dream matchup with guys named Jordan, Magic, Bird and the rest of the U.S. team that dominated the Barcelona Games in 1992.
Forget comparisons to those guys. The Americans were lucky to be better than Spain on Sunday.
Rudy Fernandez scored 22 points and Pau Gasol had 21 for the Spanish, the reigning world champions who were hoping to win their first Olympic gold.
U.S. players appreciated the game Spain gave them. After the contest they hugged the Spanish players. Bryant had an especially long embrace for Gasol, patting his Los Angeles Lakers teammate on the back.
"They did what they were supposed to do," Gasol said. "We fought hard all the way."
Seeming to appreciate the moment, after congratulating Spain, the team joined in a circle, jumping up and down at center court and waving triumphantly to the crowd as Krzyzewski applauded on the sidelines.
The Americans had won their first seven games by 30.3 points, including a 119-82 rout of Spain. But they never had control of this game, giving up open looks from the perimeter and plenty of points in the paint.
But Bryant, who waited so long to finally wear the red, white and blue, hit two 3-pointers in a big fourth quarter to add the gold medal to the only piece of hoops hardware he didn't already own. The NBA MVP pounded his hands toward the floor in celebration at the end.
James scored 14 points, while Carmelo Anthony and Chris Paul had 13 apiece for the Americans, who had won bronze medals in their last two international events, the 2004 Olympics and '06 world championships.
The U.S. started planning for this game after that first event, the low point in its hoops history, following a sixth-place flop two years earlier in the world championships. Jerry Colangelo was given control of USA Basketball and constructed a national team program in 2006, requiring those who wanted to play to commit to three years.
He got Bryant and James quickly on board and landed almost everyone else he asked for, finding a group of NBA stars eager to give up their summer to get back what they felt belonged to their country.
And he needed all of them against a Spain team that on this day would have likely beaten any other recent U.S. squad.
Jason Kidd ran his record to 56-0 in senior international play and collected another gold to place alongside the one he earned in 2000, becoming the 13th U.S. player with multiple golds.
That elite list, which includes Michael Jordan and seven other Dream Teamers, could grow in 2012. Paul and Dwight Howard said they would be in London if asked, and perhaps half this team could join them.
James ran out for pregame warmups with his finger in the air, already believing the U.S. was No. 1. But even though the Americans were shooting better than 70 percent for most of the first half, it would take a long time to prove it.
James and Bryant were both on the bench after picking up two fouls in the first 3 1/2 minutes, and though Wade came in and picked up their scoring load, the U.S. reserves couldn't open their usual cushion.
Spain hit seven of its first nine shots, leading for much of the first quarter. A quick burst of 10 points by James and Wade had the U.S. advantage up to 14 points with 4 minutes left in the half, but Spain chipped away and trailed only 69-61 at the break.
Spain was within four on a number of occasions in the third, and Fernandez's 3-pointer cut it down to 91-89 with 8:13 remaining. Bryant answered with a bucket, later added a 3, and things seemed safe when James scored to make it 103-92.
Spain made one last push to close within 108-104 on Carlos Jimenez's 3-pointer, but Wade hit one on the other end, and the final score became lopsided when the Americans hit a bunch of free throws after the Spanish became frustrated and were called for the technicals.