Carlo Ancelotti's men were in cruise control after a Cristiano Ronaldo double and a header from Karim Benzema but they flirted with disaster after giving away two penalties
The visitors looked to be on course for another big win after Cristiano Ronaldo's double, which sandwiched a Karim Benzema strike, but two penalties in as many second-half minutes from Jonathan Viera put a different complexion on the match.
Madrid held on despite late pressure, though, closing the gap on leaders Barcelona to six points, leaving their hosts rooted to the foot of the table.
Rayo boss Paco Jemez made only one change from their defeat at Osasuna, bringing in Nacho Martinez for Alberto Bueno. In contrast, Carlo Ancelotti continued to rotate his squad as he made six alterations from their win over Sevilla, with Xabi Alonso making his first start of the season.
Fresh from putting seven past Sevilla on Wednesday, Madrid appeared in clinical mood again and it took Ronaldo only three minutes to break the deadlock, as he collected Luka Modric's pass and confidently placed an effort into the bottom far corner.
That strike was his 19th in 15 matches this season, making it his best start to a campaign, and although Rayo had a goal disallowed through Iago Falque, Madrid continued to stretch the hosts' defence.
Bale set up the next two goals at Estadio de Vallecas as his right-footed cross was headed past Ruben by Benzema after 31 minutes, the Frenchman's third goal of the week.
And the Welshman squared to Ronaldo three minutes into the second half for Real's third, with the visitors seemingly in cruise control.
But the rampant visitors saw their progress halted soon after as Dani Carvajal brought down Viera in the box on 53 minutes, and the striker picked himself up to convert the penalty.
Remarkably, he was afforded the opportunity to reduce the deficit further just two minutes later - Marcelo the offender this time, fouling Falque after Joaquin Larrivey's header had come back off the crossbar. Viera kept his cool to double his tally and bring it back to 3-2, with Madrid clearly rocking.
Jemez's side thought they had equalised with 25 minutes to go, but Diego Lopez tipped Bueno's effort on to the post.
Saul missed a glorious chance to equalise in the area with just over 10 minutes to play as he knocked wide while off balance, before Ronaldo forced Ruben into a smart double save at the other end.