The Welshman scored twice, either side of a Lucas Vazquez strike, to earn the visitors all three points, despite Zinedine Zidane's men falling 2-0 behind in shocking fashion
Gareth Bale scored the winner on his return from a back problem as Real Madrid came from two goals down to beat Rayo Vallecano 3-2 and keep pace in the Liga title race, despite losing Karim Benzemato injury.
Madrid visited Vallecas without Cristiano Ronaldo because of a calf strain, but were able to welcome back Bale to the starting XI after he missed the 3-0 midweek win over Villarreal.
Zinedine Zidane's men were just a point behind leaders Barcelona going into Saturday's contest with their neighbours, but were quickly given a significant task to overcome as Rayo went 2-0 up inside 14 minutes.
Adrian Embarba broke the deadlock in the seventh minute for Paco Jemez's hosts – who were beaten 10-2 in December's reverse fixture at the Santiago Bernabeu – with Rayo's first real chance of the game.
Miku converted a corner to double Rayo's lead, but a header from Bale hauled Madrid back into the game before Benzema was brought off with an apparent injury, putting his participation in Tuesday's Champions League semi-final first leg with Manchester City in doubt.
Lucas Vazquez came on for Benzema and levelled matters in the 52nd minute with a fine header of his own, with Bale then pouncing on a stray Rayo pass to surge into space and fire under Rayo goalkeeper Juan Carlos to at least temporarily send Madrid top ahead of Atletico Madrid's clash with Malaga and Barca's game against Sporting Gijon.
Bale went close to marking his return with a third-minute goal as his fierce low effort hit the outside of the post.
But Madrid fell behind four minutes later, Bebe working his way down the left and squaring for Embarba to open the scoring with a simple close-range finish.
Madrid quickly pressed for an equaliser, with Benzema acrobatically volleying over Mateo Kovacic's lofted pass into the area.
However, Madrid's task was made more difficult in the 14th minute when Miku lashed home after Madrid had failed to deal with corner from the right.
Bale got the visitors back into the contest 10 minutes before the break by meeting Toni Kroos' right-wing corner with an excellent header into the bottom left of the net.
Benzema then came off, but Zidane's men piled on the pressure in search of the leveller, which almost arrived when Bale curled wide from the edge of the area.
Despite Benzema's withdrawal, Madrid restored parity seven minutes after the restart through his replacement.
Danilo did excellently dig out a superb right-wing cross that was met by Vazquez with a header that left the goalkeeper with no chance.
Bale sent Jese through on goal with a flick-on, only for Juan Carlos to deny the forward with a one-on-one save.
Wales international Bale continued to cause Rayo problems and his low ball from the right would have resulted in Madrid taking the lead had Vazquez been able to get a touch.
But, after Rayo defender Tito was taken off on a stretcher, Bale was finally rewarded for his strong performance nine minutes from time as he took full advantage of the hosts giving the ball away in their own half to pile the pressure on Barca and city rivals Atletico.