The holders won thanks to Karim Benzema's strike 12 minutes from time, after Sergio Sanchez and Martin Demichelis put Malaga on course for a win in the capital with goals in the 10th and 29th minutes.
Real coach Jose Mourinho said: "Our performance in the first half was not good. We lacked concentration and made big mistakes in set pieces, but my team cleaned its own trash thanks to the support of the crowd. It isn't easy to find such support when you're losing. We came back thanks to the bond between team and audience, and we'll have the advantage in Malaga."