Ronaldo's relief was clear to see when he fired home the rebound after seeing his penalty saved by Roberto 15 minutes from time yesterday.
Atletico Madrid remain tied with Real in third after they shrugged off a season of goalscoring woes by thrashing Levante 5-0 thanks to doubles from Antoine Griezmann and Kevin Gameiro.
Both Madrid sides will now be hoping Barca slip up when they travel to Valencia for a top-of-the-table clash.
"We are pleased with the result because the team showed character with time running out to score the third," said Madrid boss Zinedine Zidane.
More From This Section
"There were plenty of good things, but also things we didn't want to see."
Real looked set to shake off their recent league struggles in a dominant start as Ronaldo's header came back off the woodwork leaving the unmarked Karim Benzema with the simple task of nodding into an empty net.
Keko pounced on the German World Cup winner's gift and his cross was calmly chested down and slotted beyond Kiko Casilla by Diego Rolan.
"We can play better for sure, it is true we gave the ball away too much," added Zidane.
"We are going to be positive because today we've taken the three points, but we can do things much better.
- Real bailed out -
===================
Parity only lasted three minutes for the visitors though, as Kroos made amends with a corner from which Casemiro powered home.
Paul Baysse's header looked to have levelled with the last touch of the half, but the French defender was penalised for a push on Dani Carvajal.
Malaga did level just before the hour mark thanks to some calamitous goalkeeping from Casilla as he let Chory Castro's drive from 25 yards slip through his grasp into the bottom corner.
However, Real were bailed out when they were finally awarded their first penalty in La Liga this season when Luka Modric was tripped inside the area.
Atletico had scored just four goals in their previous six La Liga games, but ran riot at the Ciutat de Valencia for their first victory at Levante in six visits.
The hosts played a huge part in their own downfall as Rober Pier turned Gameiro's cross into his own net after just five minutes and Chema Rodriguez's botched goal line clearance then handed the Frenchman an open goal to double Atletico's lead before half-time.
"You can see that Antoine and I have a good relationship on and off the field," Gameiro told BeIN Sports Spain.
"When you don't win people talk a lot, but we are very serious and work hard every day to win games.
"Now we are in good form and we need to keep going."
Griezmann's lovely through ball was slotted home by Gameiro before he returned the favour for Griezmann to finish on the stretch.