Costa, continuing a rich vein of form that has seen him bag 22 goals in the league and become a Spain international, outsprinted the defence before beating keeper Kiko Casilla in the 55th minute.
It was a cagey game but Atletico generally carried more threat in front of goal and now have 67 points, three fewer than Real. Barcelona, third on 63 points, face Osasuna on Sunday.
"Espanyol played very well and made it very difficult for us but we were strong and knew how to cope," Costa told reporters.
"We will keep going game by game and playing as we always do."
Ronaldo is top scorer in La Liga and notched his 25th league goal of the campaign with a clinical finish from the edge of the area to see off Malaga who battled hard but lacked a cutting edge.
Isco missed an excellent chance to give Real a more convincing win against his former club but the only true downside for the leaders was the injury picked up by France striker Karim Benzema.
He hurt his thigh after colliding with Ronaldo and had to be substituted but it did not look a serious injury.
"We suffered because they pressed us in the second half and we were not able to get forward much," Real midfielder Xabi Alonso told reporters.
"We were not sharp in moving the ball from midfield to attack. We didn't link up well."
Real were without the suspended Sergio Ramos at the back and brought in Raphael Varane.
Angel Di Maria, one booking away from a suspension and with the 'Clasico' looming next weekend against Barca, was on the bench and Isco started.
Malaga went out looking to assert themselves and they had plenty of the ball, Nordin Amrabat a threat with his movement and energy.
The Andalusian side have had defensive problems all season, though, and they stood off Ronaldo too much, giving him time to pick his spot in the bottom corner of the net after 23 minutes.
The Portuguese forward could have added a quick second goal but was denied by a good block from Willy Caballero.
Di Maria was introduced after half an hour for Benzema and Real started to play at their rhythm.
Isco was sent clear by Di Maria after the break but failed to keep his cool in front of goal and fired over.
Second-placed Atletico were missing suspended duo Joao Miranda and Mario Suarez while Espanyol were able to put out their first choice 11.
Espanyol have found success this season with a solid formation but they lack creativity going forward and the opening stages against Atletico was a game of attrition as neither side ceded much space.
Gradually openings began to appear mainly for Atletico as David Villa went close with a couple of headers and Gabi volleyed over from a corner, while at the other end Thibaut Courtois made a smart save to block a deflected Pizzi strike.
Costa made the breakthrough as he went past Diego Colotto with a burst of speed and placed the ball beyond the dive of Casilla.
Atletico looked shaky in the final stages and they needed Courtois to parry a dangerous drive from David Lopez.
Elsewhere, Rayo Vallecano beat Almeria 3-1 and Levante went down 1-0 at home to Celta Vigo.