Third-from-bottom Mallorca twice went in front with headers against a seemingly distracted Madrid at the Bernabeu before the hosts rallied.
Cristiano Ronaldo, Luka Modric and Gonzalo Higuain, with his second of the game, netted in a six-minute spell and substitute Karim Benzema notched a fifth goal at the end to leave coach Jose Mourinho's side in second place with 61 points and 10 games left.
"In the first half we lacked intensity but we were brilliant in the second," Madrid director Emilio Butragueno told Spanish television.
"The changes by the coach made the team much more offensive."
French winger Antoine Griezmann scored twice, the second with a spectacular volley, as Real Sociedad battered visiting Real Valladolid 4-1 to go fourth with 47 points.
Sociedad are two ahead of Valencia who climbed to fifth with a 3-0 win over 10-man Real Betis.
Valencia took the lead with a Roberto Soldado penalty in the 16th minute, after Betis defender Antonio Amaya fouled him on the edge of the area and was sent off.
Five minutes from time Betis defender Paulao turned a Sergio Canales cross into his own net and Jonas completed the scoring with a curling shot at the end.
Getafe are eighth with 42 points, behind seventh-placed Betis, after Borja scored the only goal in a 1-0 home win over Athletic Bilbao.
Mallorca struck first at the Bernabeu when the lively Giovani dos Santos whipped in a cross and Emilio Nsue nodded the ball in from the edge of the area in the sixth minute.
Madrid lined up with an unfamiliar midfield that included Modric and Pepe and the two combined well to help set up Higuain for the equaliser in the 15th minute.
Mourinho's team have often struggled at set pieces this season and they were undone again in the 21st minute when Alejandro Alfaro was left unmarked at the far post to head home a flicked-on corner.
Madrid displayed a different attitude after the break with Mesut Ozil replacing youth-team striker Alvaro Morata. A corner from the German found Ronaldo in the 52nd minute and he nodded his 27th league goal of the season.
Modric fired Madrid in front from long range two minutes later and Ozil dribbled to the touchline before pulling the ball back for Higuain to make it 4-2.
Argentine Higuain then squared the ball for Benzema to walk the fifth goal into an empty net in stoppage time.
Sociedad extended their unbeaten league run to 10 games with a devastating display of attacking football against Valladolid that kept them on course for a shot at European football next season.
After a tight opening period Sociedad scored three times in the last 13 minutes of the first half, the 21-year-old Griezmann tucking away the first from close range in the 33rd.
Imanol Agirretxe curled a shot into the top corner soon after and Griezmann brought the house down with his strike just before
Get the best features, fun and footballing frolics straight to your inbox every week.
Thank you for signing up to Four Four Two. You will receive a verification email shortly.
There was a problem. Please refresh the page and try again.