Thierry Henry's side took an 11th-minute lead through Enzo Millot, but Spain responded with three goals in the first half, including a brace from Fermin Lopez, to turn the match around.
Fermin's first came in the 18th minute, before he added a second in the 25th, and Alex Baena then got in on the act in the 28th minute when he found the back of the net with a clever free kick.
France scored a second in the 78th minute through Maghnes Akliouche, who got a touch on a Michael Olise free kick, and Henry's team were then awarded a penalty in the final moments, with Benat Turrientes pulling Arnaud Kalimuendo to the ground. The referee initially said no, but he gave the decision following a VAR check, and Jean-Philippe Mateta stepped forward to level the scores at 3-3 in dramatic circumstances.
Spain scored a fourth in the 100th minute, though, with Sergio Camello expertly lifting the ball past France goalkeeper Guillaume Restes after collecting a pass from Adrian Bernabe, and the same player then added another in the final moments following a quick break.
Spain survive France comeback in Paris
Spain have followed up on their gold medal in the men's football at the 1992 Olympics, and Santi Denia's side were deserved winners at the Paris 2024 Olympics despite being given a late scare.
France were looking for their first gold medal since 1984, but a silver still represents a strong showing for the host nation despite falling just short of the main prize in front of a vocal home support.
The breakthrough came in the 11th minute of the contest, with Millot finding the back of the net courtesy of a huge mistake from Spain goalkeeper Arnau Tenas.
It looked to be a relatively comfortable save for Tenas, with Millot's strike from just inside the box lacking power, but it somehow ended up in the corner, with the Spain stopper palming it into his own goal.
However, Tenas managed to make amends late in the first period, producing a stunning save to keep out a header from Mateta, before Olise just missed the target shortly before the whistle.
Tenas, who joined Paris Saint-Germain from Barcelona last year, made another big stop late on to deny Kouadio Kone, but France scored twice in the latter stages to force extra-time.
Man of the Match: Fermin Lopez
Spain levelled it at 1-1 in the 18th minute, with Barcelona midfielder Fermin brilliantly picking out the bottom corner after being found by Baena, as two members of Spain's Euro 2024-winning squad combined.
Fermin was at it again in the 25th minute, converting from close range after Abel Ruiz had seen his close-range effort kept out by France goalkeeper Restes.
Spain then had a third in the 28th minute, with Baena bending a free kick into the top corner; France were stunned, and it was a real battle for the hosts to work their way back into the contest, but they achieved it.
Fermin, who played once for Spain at Euro 2024, scored 11 times for Barcelona last season, and the hugely talented 21-year-old has ended the Olympics with six goals; he was replaced in the 72nd minute of Friday's final but was still the star of the show, closely followed by Mateta.
France were relying on moments of individual quality, with Olise looking to get on the ball whenever possible, but Spain, much like their team at Euro 2024, were careful with their work, continuing to look for spaces in the final third, and they are comfortably the dominant outfit in the first half in Paris.
Alexandre Lacazette found it so tough to make his mark, and he was replaced in the early stages of the second period, with France looking for some sort of response; the hosts forced extra-time with a late showing, but all of their hard work was ultimately for nothing.
The future is bright for Spain
Spain were deserved winners of Euro 2024, and they could be the team to beat in the coming years, as a number of youngsters have enjoyed an excellent Olympics.
Pau Cubarsi will again be looking to secure regular minutes for Barcelona next season and can star for the national side in the future, while Baena and Fermin are also capable of being key players.
Spain were tipped to enjoy a strong Olympics due to the strength of their squad, and much like in the European Championship, there can be little argument that the best side has triumphed in this competition.
France must be praised for their late comeback to force extra-time, but Turrientes smashed the crossbar in the latter stages of normal time, as he came close to atoning for his penalty error, and Spain then won it in extra-time courtesy of a brace from late substitute Camello.