Mallorca had looked on course to claim a shock win after Tomer Hemed gave them a 39th-minute lead but Madrid drew level through substitute Gonzalo Higuain 18 minutes from time before Jose Callejon netted an 84th-minute winner for Jose Mourinho's men.
The victory was Madrid's 22nd in their last 23 competitive matches and lifted them further clear of Barca, who play Real Betis on Sunday.
The comeback win will also give Madrid a further morale boost ahead of Wednesday's Copa del Rey quarter-final first leg against Barca at the Bernabeu.
Mourinho remarked: "We faced a team with a great coach that organised itself perfectly in defence. They knew what our strengths and our style are. They were very focused and all their men played together waiting for a counterattack. I think they had a fantastic first half. You sometimes have to credit your opponent."