First-half goals from Rodri and Dani Olmo put La Roja in control, before Rodrygo took advantage of a defensive blunder to pull one back for the Selecao five minutes before half time.
Teenage sensation Endrick then came off the substitutes' bench to restore parity for Brazil just five minutes after the interval, before Manchester City lynchpin Rodri thought he had won it for Luis de la Fuente's side when he converted his second penalty of the contest in the 87th minute.
However, West Ham United's Lucas Paqueta had the last laugh as he stepped up to slot home a dramatic 96th-minute penalty to force both nations to share the spoils in Madrid.
After being rested for Spain's 1-0 defeat to Colombia last weekend, Rodri was recalled to the starting lineup against Brazil, and he did not take long to make his mark as he opened the scoring from the penalty spot in the 12th minute.
The spot kick was awarded to the hosts after 16-year-old Lamine Yamal was brought down by Brazil's Joao Gomes, and Rodri put Spain ahead with a composed finish straight down the middle.
Nico Williams and Fabian Ruiz both failed to hit the target during a dominant spell from Spain, but it took a moment of magic from Olmo to eventually double the hosts' advantage in the 36th minute.
The RB Leipzig attacker received the ball inside the penalty box and nutmegged one Brazilian before dancing past another and curling a superb left-footed finish into the far corner, much to the appreciation of the starstruck home supporters.
Spain were in cruise control, but Dorival Junior's Brazil made things interesting on the stroke of half time when Real Madrid forward Rodrygo - playing on familiar territory at the Bernabeu - capitalised on a calamitous mistake from goalkeeper Unai Simon before coolly chipping the Athletic Bilbao man from the edge of the box.
Brazil made four changes at the interval, including the introduction of 17-year-old soon-to-be Real Madrid striker Endrick, who scored the match-winner in a 1-0 victory against England at Wembley Stadium last weekend.
The teenage starlet did not take long to make his present felt, as he restored parity for the South American giants just five minutes after the restart when he pounced on an attempted headed clearance from Aymeric Laporte following a corner and fired a left-footed volley through the gloves of Simon into the bottom corner.
De la Fuente's side continued to control the majority of possession, but they were wasteful in the final third, while Brazil looked a threat on the counter-attack.
Spain's persistence in the final third was eventually rewarded, though, as referee Antonio Nobre pointed to the penalty spot after Dani Carvajal, who met a perfectly-weighted through-ball into the box from Yamal, was brought down by Lucas Beraldo.
After placing his first spot kick down the middle, Rodri decided to smash his second effort from 12 yards into the far left, sending Bento the wrong way as he celebrated scoring just his third international goal on his 49th appearance for La Roja.
However, Brazil refused to go down without a fight and drama unfolded deep into stoppage time when substitute Galeno - who had only been introduced just over 10 minutes earlier - was dragged to the ground by Carvajal, who grabbed the Brazilian's leg to give Nobre no choice but to award a third penalty of the match.
The pressure was on - albeit in a friendly - for Paqueta to score with virtually the last kick of the game, and the West Ham man made no mistake from 12 yards, taking a hop, skip and a jump before sending Simon the wrong way to rescue a draw for Brazil.
Both nations are next in action in June, with Spain taking on European minnows Andorra in a friendly on June 4, while Brazil face Mexico three days later.