Barcelona were unbeaten in the previous seven encounters between the two sides in all competitions and had not lost to Real in LaLiga since 2016, but Los Blancos rose to the occasion and put an end to their recent poor form.
A loss at Levante was followed by another defeat to Manchester City in the Champions League in midweek but Zinedine Zidane's side found the perfect response on Sunday night.
That was before Vinicius made the breakthrough in the 71st minute and substitute Mariano wrapped up the three points in injury time.
Barcelona boss Quique Setien also made two changes from the team that started the draw at Napoli in the Champions League. Junior Firpo dropped down to the bench as Jordi Alba returned from injury and Arthur replaced Ivan Rakitic.
Lionel Messi's inclusion saw him make a landmark appearance as he became the first Barcelona player to feature in 43 Clasico fixtures, overtaking his former team-mate Xavi.
Much of the first half was characterised by long spells of possession for Barcelona, but a lack of urgency meant it was without much threat to the Madrid goal.
Madrid's build up play seemed sharper and more incisive and Karim Benzema was inches away from getting a telling touch to Federico Valverde's driven, low cross following a swift counter attack from the hosts.
Messi had been on the periphery of the match for the opening half an hour but suddenly came into the game. First he worked Courtois from a tight angle on his weaker right foot, before breaking clear only to be denied by the goalkeeper again when one-on-one.
The Belgian made a third fine save in quick succession, alertly coming off his line to block Arthur's effort when he was through unchallenged.
Early in the second half it was the turn of Ter Stegen to produce a world class save as he flung himself to his left to divert Isco's curling shot away from the top corner and wide of the post.
Isco managed to beat Ter Stegen on the hour mark but his header was cleared off the line by Gerard Pique.
Madrid were piling on the pressure and Karim Benzema should have put them in front when he was picked out at the far post by Dani Carvajal, but the striker volleyed the ball wide from six yards out.
Setien introduced new signing Martin Braithwaite from the bench after 69 minutes and the striker's pace immediately gave Barcelona another option as he raced through on goal, only for Courtois to make another huge save.
It proved a vital moment as Madrid went down the other end and opened the scoring. Kroos was given time to pick a pass into Vinicius, who advanced into the penalty area down the left before beating Ter Stegen with a shot that deflected off Pique and into the net at the near post.
Pique had a glorious chance to equalise after 83 minutes when Messi picked him out with a quick free-kick, but the defender got his header wrong and the ball flew harmlessly wide.
Madrid withstood some late pressure but held firm in defence and ensured victory in injury time when Mariano, with his first touches having just come on for Benzema, ran past Barcelona centre-back Samuel Umtiti and beat Ter Stegen with a shot from a tight angle.