Hansi Flick’s team came back from behind to maintain their winning start to La Liga, and Dani Olmo scored his debut winner just hours after he was officially registered for Barcelona.
Olmo returned to Barca from RB Leipzig for an alleged 60 million euros (£51 million) earlier this month, but the stringent financial regulations of the Spanish top division prevented him from playing.
However, the 26-year-old was able to play at Rayo Vallecano after Clement Lenglet, the defender, transferred to Atletico Madrid and Ilkay Gundogan was released back to Manchester City.
And late in the game, following excellent work from fellow Spain Euro 2024 hero Lamine Yamal, the playmaker found the winner by curling in left-footed.
After three games, Barca is the only team remaining with a perfect record in La Liga.
The home team had an early lead thanks to Unai Lopez, which they maintained until Pedri and Raphinha executed a brilliant one-two and Pedri scored.
Olmo, who was at Barcelona as a boy but left for Dinamo Zagreb at the age of 16 without making an appearance, then stole the points and the headlines with his fantastic finish.