An enthralling contest from start to finish saw the Paraguayan emerge from the bench to see off the champions, who now see themselves embroiled in a battle for third place
Roque Santa Cruz was the match winner off the bench for the home side, scoring twice in the second half to heap more pressure on Jose Mourinho, who now sees his team fall 16 points behind league leader Barcelona.
Isco fired Malaga into a deserved lead four minutes into the second half, but a scramble in the box saw Madrid draw level in the 69th minute through a Sergio Sanchez own goal.
But Santa Cruz would make the difference for the Andalusians, scoring twice in three minutes from a pair of Joaquin assists to put los Blancos to the sword, rendering Karim Benzema's 82nd minute strike nothing but mere consolation.
Third-placed Madrid now sees its title hopes all but disappear, and must now cast wary glances over its shoulders as Malaga, with the victory, moves two points behind the capital outfit.
Mourinho surprised everyone by dropping captain Iker Casillas to the bench, giving Antonio Adan the unlikeliest of starts between the posts for the Spanish champions.
But it was Malaga keeper Wily Caballero who was called into action first, the Argentine showing excellent reflexes to thwart Cristiano Ronaldo’s free kick from 30 yards out only a few minutes into the game.
The match proceeded at a frantic pace, with both teams not shying away from contact in midfield, though the best chances would continue to fall Madrid’s way throughout the first half.
The capital outfit should have been ahead with 24 minutes on the clock, but Ronaldo, after slipping in Angel Di Maria on the right, blasted the return ball over the bar from a gilt-edged position at the far post.
The pair would combine again almost immediately on a scintillating Madrid break. From the left flank, the Portuguese played in Di Maria between the center backs with a fantastic pass, but the winger’s clever dink over Caballero was hacked off the line by Nacho Monreal.
Halftime was a welcome respite for Malaga, which was forced increasingly onto the ropes coming up to the interval. But it emerged from the break firing on all cylinders, and four minutes after the restart, stormed into the lead.
Joaquin did well to manufacture space on the left flank and pick out Isco just inside the area, and the 20-year-old took one touch before finishing superbly into the bottom corner across the body of Adan.
Malaga was creating all the danger in the second half, and Joaquin fired just wide after an inspired run down the wing, before Javier Saviola brushed the frame of the goal with an attempt from the edge of the box.
But Madrid rode its luck at both ends of the pitch, and found a way back into the match with 21 minutes remaining. Ronaldo’s cross to the far post sparked an almighty scramble, which ended when Sanchez’s outstretched leg sent the ball trickling past the wrong-footed Caballero.
The Malaga defender would atone for his mishap within moments of the restart though, as he threw himself at Mesut Ozil, who was primed to score from point blank range after a marauding run.
And in the 73rd minute, the home side would be back in front, as a low cross from Gamez was flicked by Joaquin into the path of Santa Cruz, who tapped into an empty net at the far post.
Within minutes, Malaga was 3-1 ahead, with Joaquin again pulling the strings in attack. The winger played in Santa Cruz with a lovely reverse pass, and the substitute’s finish into the bottom corner was equally sublime.
But just when the hosts looked assured of victory, Ozil picked out Benzema on the counter, and the Frenchman finished calmly to mark a nervy end to the game with eight minutes remaining.There was nothing left from Madrid though, as Malga defended superbly in the time that remained to clinch a priceless scalp, and move to within two points of third place.