
Hansi Flick's side were made to work for the points after falling behind early, while Sergio Camello scored twice for Rayo to keep the visitors in the contest for long periods.
Rayo had failed to score in their previous four visits to Barcelona, but they stunned the home supporters inside 12 minutes.
Alvaro Garcia broke forward down the left before pulling the ball back for Camello, who fired a powerful effort into the top corner.
Barcelona responded quickly and Raphinha soon brought the hosts level. The Brazilian collected a pass from Xavi Espart, beat the final defender and calmly rolled his finish past goalkeeper Daniel Cardenas.
The home side continued to push forward and took the lead soon afterwards. Marc Bernal found Yamal inside the penalty area, and the teenager cut onto his left foot before firing past Cardenas at the near post for his first goal of the 2026/27 campaign.
Barcelona remained in control for much of the first half, although Pedri and Anthony Gordon were unable to convert further chances. Jules Kounde also had a goal ruled out for offside shortly before the break.
Flick's side extended their advantage six minutes into the second half. Dani Olmo played Anthony Gordon through, and the winger sent the ball across the face of goal, with Rayo defender Florian Lejeune turning it into his own net.
However, Rayo refused to give up.
Camello scored his second goal of the night before the hour mark, rising highest to head home an Unai Lopez corner and reduce the deficit to 3-2.
The visitors almost drew level moments later when Joan Garcia struggled to deal with a volley from Fran Perez. Camello was ready to complete his hat-trick, but Pau Cubarsi produced a crucial block to deny him.
Barcelona eventually regained control, with Gordon playing a key role again. The England international collected a long pass, cut inside and produced a clever backheel for Raphinha, who drilled home his second goal of the match and his fifth of the season.
Yamal then completed the scoring after being set up by Karim Adeyemi. The teenager curled a fine finish into the bottom corner to complete his brace and seal a 5-2 victory.
Barcelona have now won all three of their league matches this season and extended their remarkable home league winning run to 21 matches.
Despite the victory, Flick made it clear that he wants his players to improve their control of matches.
"The first half we played very well. The reaction after the goal has been very good," Flick said.
"When we're winning 3-1 we have to control the ball better because we have the quality to do it. It's good to score, but with the ball we need to recover. We have to make the ball run.
"It has been a very tough rival. We have many matches left and even if we win 3-1, we have to learn to control the game. It's good that they want to score, but we have to control the game."
Barcelona will look to continue their perfect start when they face Valencia at the Mestalla on Sunday.








