Matheus Cunha took advantage of a defensive lapse to score in first-half stoppage time after Richarlison smashed a penalty over the crossbar for Brazil.
Spain equalised on the hour when Mikel Oyarzabal crashed home a thumping volley, but former Barcelona forward Malcom secured Brazil the title with his goal in the 108th minute.
Brazil, who won gold on home soil at Rio 2016, became the fifth team to retain their Olympic crown on another triumphant night in Yokohama, where they claimed the last of their five World Cup titles in 2002.
After Neymar led Brazil to Olympic gold, this time it was the turn of Dani Alves to captain the country in the final against a strong Spain side featuring six players who participated at Euro 2020.
Unbeaten at the Olympics against a European nation since 1988, Brazil almost fell behind when Diego Carlos scooped the ball over his own goalkeeper before hooking off the line as Oyarzabal tried to pick out Dani Olmo.
A poor clearance from Unai Simon allowed Richarlison to set up Douglas Luiz, whose spinning effort was turned behind by the Spain goalkeeper.
Brazil needed penalties to get past Mexico in the semi-finals, and they were awarded a spot-kick here after a VAR review as Simon flattened Cunha while attempting to punch away a cross.
But Everton forward Richarlison wasted the chance to add to his competition-leading five goals by blazing well over.
There was no let-off for Spain a second time though when Alves sent the ball high into the area, where a hesitant Pau Torres gave Cunha time to control and slot into the corner.
Marco Asensio, Spain's match-winner in extra time against Japan in the previous round, lasted just 45 minutes as Carlos Soler came on for the second half, inadvertently getting in the way of a goalbound effort from Oyarzabal.
Brazil nearly doubled their lead when a quick break resulted in Simon making a terrific stop with his legs to divert Richarlison's shot onto the bar.
Spain youngster Bryan Gil, who will join Tottenham Hotspur on his return from Japan, rattled the bar two minutes from the end, as an extra 30 minutes was required for the fifth time in eight knockout matches.
Malcom, who endured a difficult spell at Barcelona in 2018-19 before leaving for Zenit Saint Petersburg, ran onto a long ball from Antony, racing past Jesus Vallejo and firing past Simon to etch his name into the history books.
Highlights Olympics Tokyo Brazil