Brazilian winger Vinicius received racist abuse against Valencia on Sunday, but was watching on from the president’s box because of a knee problem.
Rodrygo struck in the 89th minute to send Madrid second and bowed his head while lifting his fist in a “Black Power” salute.
Karim Benzema sent Madrid into the lead in the first half before Raul de Tomas equalised for the visitors at the Santiago Bernabeu, with Rodrygo deciding the game in the final stages.
Vinicius was not present on the pitch but was not far from the players’ thoughts, as Madrid lined up before the game wearing his number 20 shirt as a gesture of support.
The forward was sent off against Valencia and although the card was revoked on Tuesday night, he was not able to recover from his knee issue to play.
Supporters chanted his name after 20 minutes and a banner in one stand read “We are all Vinicius, enough already!”
“I think everyone in football, not just in football, in all of sport, the whole world, (is reacting to) these actions, they should not be allowed,” Madrid defender Dani Carvajal told Movistar after the game.
“We have to all improve, try to make this not happen, because in the end it’s not fair.”
Benzema broke the deadlock by taking the ball around the goalkeeper after Fede Valverde played him through.
Madrid created few further notable chances in a drab performance, with Carlo Ancelotti’s side without a clear goal in the final weeks of the season.
Rayo struck back through De Tomas, and the former Real Madrid youth player did not celebrate his goal.
Rodrygo ensured Madrid had the last laugh and brought Vinicius back into focus with his celebration.
Elsewhere Villarreal kept their slim Champions League qualifying hopes alive with a 2-0 win over struggling Cadiz, closing the gap on Real Sociedad to five points.
Later Wednesday relegation-battling Espanyol host Atletico Madrid, who can overtake their city rivals and return to second.
AFP