Sergio Camello came off the bench to score twice in extra time as Spain added Olympic gold to their glittering trophy cabinet with a thrilling 5-3 victory over France.
Three goals in 10 first-half minutes – two from Fermin Lopez and the other from Alex Baena – appeared to take the final away from Thierry Henry's hosts, who had led early on through Enzo Millot.
However, Maghnes Akliouche teed up a grandstand finish at the Parc des Princes, then Benat Turrientes' foul allowed Jean-Phillipe Mateta to force extra time with a 93rd-minute penalty.
Both teams threw caution to the wind in the additional period, but Camello was the hero, producing a wonderful lob and a breakaway second to secure gold for Spain.
The hosts took the lead with the very first attempt of the game as Millot pounced on a loose clearance to fire goalwards from the right side of the area. Spain goalkeeper Arnau Tenas seemed to misjudge the flight of the ball, palming it into the bottom-left corner.
However, Spain soon hit back through Barcelona youngster Lopez, who swept his finish into the bottom-left corner after being found in the middle of the area by Baena.
Buoyed by that strike, Spain built a two-goal lead within 10 further minutes. Lopez pounced for his second when Guillaume Restes could only parry Abel Ruiz's shot to put them 2-1 up, then Baena added the pick of the goals.
Lining up a free-kick just to the left of the D, the Villarreal man gave Restes no chance by whipping a sensational effort over the wall and into the top-left corner.
Manu Kone missed a great chance to cut Spain's lead when he nodded against the crossbar after the break, but Les Bleus' hopes were rekindled with 11 minutes to play as Michael Olise's free-kick was steered into the far corner by Akliouche.
There was more drama in stoppage time, with Turrientes clumsily dragging Arnaud Kalimuendo down from a corner. Mateta kept his nerve from the spot to level, though Turrientes almost redeemed himself with a dream winner when his shot clipped the crossbar at the other end.
France continued to press in extra time, but they were caught out in the 100th minute as Camello latched onto Adrian Bernabe's pass before sitting Restes down with a delicate chipped finish.
Camello was not done there, latching onto a long throw from Tenas as France threw players forward in stoppage time, before making sure of the gold by finishing past Restes.
Data Debrief: Roja end 32-year Olympic wait
Since an iconic Spain team featuring the likes of Pep Guardiola and Luis Enrique won gold on home soil at Barcelona 1992, La Roja have been reduced to the role of nearly men at the Games.
They were losing finalists at Syndey 2000 (versus Cameroon) and Tokyo 2020 (against Brazil), but they got over the line this time around, even if they had to go the distance.
With the senior squad winning a record-breaking fourth European crown last month and the under-23s claiming gold in Paris, it looks like this era of Spanish success could go on for some time yet.