The hosts lost Gareth Bale to an early injury but broke the deadlock when captain Sergio Ramos converted James Rodriguez's free-kick.
Colombia playmaker Rodriguez impressed throughout and netted a spectacular second in the 69th minute, shortly after winning a penalty that Cristiano Ronaldo uncharacteristically blasted against the post.
Juanmi, who scored the winner for Malaga when they stunned Barcelona at the Camp Nou in February, reduced the deficit in the 71st minute but Ronaldo atoned for his earlier miss in stoppage time.
Carlo Ancelotti's team close to within two points of Barca at the summit after their rivals beat Valencia 2-0 on Saturday, although the injury to Bale and a later knock for Luka Modric leaves the coach with plenty to ponder ahead of Wednesday's UEFA Champions League quarter-final second leg against Atletico Madrid.
Real headed into the match with their forward line depleted due to Karim Benzema's knee injury and there were further problems in attack for Ancelotti during the opening moments.
Under-fire Wales international Bale went to ground clutching his calf and limped off to be replaced by Javier Hernandez in the fifth minute.
On-loan Manchester United striker Hernandez celebrated a rare start by scoring in last weekend's 3-0 win over Eibar and he almost found the net once more as Carlos Kameni brilliantly tipped the Mexican's header from Rodriguez's free-kick over the crossbar.
A set-piece proved to be Malaga’s undoing in the 24th minute, as Rodriguez fired in another excellent delivery for Ramos to volley home from point-blank range – Malaga's offside appeals falling on deaf ears.
Rodriguez continued his fine start to the match with a venomous 18-yard strike, from which Kameni saved superbly.
Ramos and Rodriguez had the ball in the net twice in quick succession, only to fall foul of Malaga's risky offside trap, but the visitors should have been level eight minutes before half-time.
Marcelo failed to deal with Arthur Boka's cross but Sergi Darder lashed his shot wastefully over from 10 yards.
Ancelotti looked on anxiously as another key man, Modric, sustained a blow to his right knee in a challenge with Malaga midfielder Recio and soon made way for Asier Illarramendi.
Real's Toni Kroos made a vital block from Boka's attempt on the end of a cleverly worked corner before Ronaldo failed to secure a two-goal cushion in the 67th minute.
Rodriguez drew a trip from Samu Castillejo but, as Kameni guessed correctly, Ronaldo steered his spot-kick against the base of the upright.
The 2-0 scoreline duly arrived two minutes later – Ronaldo teeing up Rodriguez to complete a flowing move with an emphatic strike into the top corner from the edge of the area – but Malaga found a swift response.
Once more they found joy down the Madrid left and Juanmi was given ample room to head Roberto Rosales' cross past Iker Casillas.
A frantic finish ensued but Madrid's greater attacking firepower ultimately told as Ronaldo tucked home a cutback from the tireless Hernandez to seal victory.
Thank you for reading 5 articles this month* Join now for unlimited access
Enjoy your first month for just £1 / $1 / €1
*Read 5 free articles per month without a subscription
Join now for unlimited access
Try first month for just £1 / $1 / €1