Gerard Pique's towering header put the hosts in front on the night they honoured legendary former player and coach Johan Cruyff.
Yet, Karim Benzema quickly levelled before Ronaldo fired home five minutes from time after Madrid captain Sergio Ramos had seen the 21st red card of his career.
Victory reduces Barca's lead over third-placed Madrid to seven points with seven games remaining.
Atletico Madrid closed the gap on the leaders to six points with a 5-1 thrashing of Real Betis earlier on Saturday.
“I liked everything from my players both defensively and offensively against a great team with very good players,” said Madrid boss Zinedine Zidane after a successful first Clasico in charge. “When I see the team like this, fighting for their teammates, it is the best thing for a coach.”
There was an emotive homage to Cruyff before kick-off with over 90,000 fans holding up a display reading “Thank you Johan” alongside a Barca jersey with the number 14 the Dutch great adorned during his playing career.
Yet, it was a sad end to a spectacular run for the European champions as they tasted defeat for the first time since losing at Sevilla in October.
“Their first goal damaged us and disorientated us and caught us out of position,” said Barca coach Luis Enrique. “One day it had to happen and it happened today. Now we have to prepare for the Champions League, rest and keep going.”
Barca started by far the better and only an incredible miss by Luis Suarez let Madrid off the hook on 10 minutes.
Neymar unselfishly squared for his Uruguayan strike partner, but Suarez failed to make any connection from his low cross with the goal at his mercy.
The hosts were enraged midway through the half when Lionel Messi looked to have been brought down by Ramos right on the edge of the area, but referee Alejandro Jose Hernandez waved play on with Ramos already having been booked.
Madrid grew into the game as the first half progressed and finally threatened the Barca goal when Ronaldo's shot was beaten away by Claudio Bravo before Gareth Bale blazed a volley over from a narrow angle.
Barca started on the front foot again after the break and Navas made a brilliant save to prevent Messi's cheeky chipped effort floating in at the farpost.
But that save only momentarily denied Barca as Pique rose highest to power home Rakitic's corner on 56 minutes.
The lead lasted just six minutes, though, as Benzema swivelled to blast home Toni Kroos's deflected cross into the bottom corner.
Bale had the chance to turn the game on its head, but his tame effort on his favoured left foot was easily held by Bravo as Madrid again threatened on the counter-attack.
The Welshman did have the ball in the net nine minutes from time, but was incredibly unfortunate to be penalised for a push on Jordi Alba as he headed in.
Ronaldo then fired against the bar and Bravo saved a one-on-one with Bale as Madrid's pace on the break began to tear a tired-looking Barca apart.
The visitors' hopes of a famous win looked to have gone when Ramos was finally given his marching orders for a second booking after chopping down Suarez seven minutes from time.
However, just two minutes later Ronaldo struck the winner as he controlled Bale's cross to the far post before slotting under Bravo