Vinicius Junior drove in by using a deflection in a second half packed with possibilities after Isco saw a shot saved and an effort cleared off the goalline.
Karim Benzema volleyed over for Real and substitute Martin Braithwaite went close for Barca moments before Vinicius sparked celebrations at the Bernabeu.
Substitute Mariano struck in added time as Real moved a point clear of Barca.
Mariano had not played a single minute in La Liga for Zinedine Zidane’s side this season till being introduced off the bench in injury time to seal a memorable win.
Los Blancos finished third last season – 19 points in the back of champions Barca – however the tight nature of the title race this time around placed added significance on the fixture in the Spanish capital.
Zidane had stated his side were in a “delicate moment” as a result of picking up one point from their remaining two league matches and having suffered defeat to Manchester City in the Champions League in midweek.
And Real’s task seemed ominous given they had not beaten Barca in the league in their closing seven attempts.
But when 19-year-old VinicIus became the youngest scorer in a league fixture between the sides since Lionel Messi in 2007, they never looked back.