Madrid had lost their previous two games in all competitions but have now extended their lead at the top of the table to four points over Sevilla and five on Barcelona, with their two closest title rivals not playing until Sunday.
Ramos scored twice in the final 10 minutes of the first half having been set up by two Toni Kroos set pieces. That moved the defender on to six league goals, already his best return in a league season.
Juanpi grabbed a goal back for Malaga in the second half to set up an exciting finish, but Madrid held on despite Cristiano Ronaldo squandering chances to put the result beyond doubt.
Malaga stay 13th after their fourth consecutive La Liga defeat. They have not won an away top-flight match since March, a run spanning 13 games.
Karim Benzema – in as one of three Madrid changes – missed two early chances created by Lucas Vazquez, his header bouncing over before he attempted an ill-advised flick from a good shooting opportunity.
Ronaldo was also guilty of a poor finish, Kroos' through ball sending the forward racing clear, but he could not beat goalkeeper Carlos Kameni with a tame strike.
Malaga almost punished those misses, with Chory Castro collecting Adalberto Penaranda's pass and firing an impressive strike against the post from 18 yards, with Keylor Navas well beaten.
Zidane had to make a tactical reshuffle when Marcelo suffered an injury, with Isco coming on, but that did not stop Madrid from taking the lead on 35 minutes.
Kroos – La Liga's top assist-maker – floated in a corner that was powerfully headed in by Ramos from 12 yards.
And that duo combined again three minutes before the break, Kroos' inswinging free-kick touched in by a stretching Ramos from inside the six-yard box, despite Malaga claiming for offside.
It could have been three before half-time, with Malaga defender Mikel Villanueva relieved after he deflected Benzema's cut-back against his own crossbar.
But the visitors got one back in the 63rd minute. Juankar drove inside from the left and forced Navas to parry, with Juanpi staying cool to pick his spot from the rebound and claim his first league goal since August.
Malaga almost grabbed an immediate equaliser in their next attack, Castro bursting through to force a good diving save from Navas.
Madrid could have sealed the victory in bizarre fashion when Kameni completely missed Martín Demichelis' backpass and frantically slid to stop the ball crossing the line, before saving a follow-up effort from the onrushing Ronaldo.
Ronaldo's frustrating afternoon continued when he chested down Isco's pass and fired an effort against the post.
Kameni again kept out the Portugal international from a tight angle in Madrid's next attack as the hosts settled for a narrow - but important - victory.