Los Blancos made two changes to the side that thrashed Deportivo Alaves, as Toni Kroos and Nacho were recalled while Marco Asensio and Vinicius Junior flanked Karim Benzema upfront.
Madrid almost had a perfect start after Asensio went through on goal from Dani Carvajal's defence-splitting pass but he was denied by goalkeeper Luca Zidane in a one-on-one.
Carlo Ancelotti's charges continued to create chances but they did not have the finishing touches and Rayo almost punished them ten minutes before the half-time break after Alvaro Garcia latched onto a loose ball in the box but he fired his strike over the crossbar.
However, Madrid had a goal chalked off by the Video Assistant Referee (VAR) for offside just before the half-time break after Casemiro bundled a close-range strike into the back of the net and Zidane kept Rayo in the game on the stroke of half-time as he kept out Benzema's close-range header from Luka Modric's free-kick.
Rayo were not giving Los Blancos spaces with a deep block in the early stages of the second stanza and they nearly went ahead from a set-piece on the hour-mark after Sergi Guardiola powered a header that produced an acrobatic save over the crossbar by shot-stopper Thibaut Courtois.
However, the visitors were still dangerous in attack as Modric came close to breaking the deadlock 15 minutes before time with a strike in the box that came off the post after a reverse pass by Benzema.
Ancelotti brought on Rodrygo in the last ten minutes in place of Asensio for fresh legs upfront and Benzema eventually found the winner three minutes later with a close range finish after a through ball by Vinicius Junior, as Madrid went nine points clear at the top of the log.