Baku: Oscar Piastri won the Azerbaijan Grand Prix as McLaren went top of the constructors' standings in a race that finished with a virtual safety car after a penultimate lap collision between Ferrari's Carlos Sainz and Red Bull's Sergio Perez.
Ferrari's pole-sitter Charles Leclerc, who was overtaken by Piastri on the 20th of 51 laps and then battled nose-to-tail before his tyres faded, came second with George Russell inheriting third for Mercedes after the Sainz-Perez collision.
Red Bull's Formula One leader Max Verstappen finished fifth, just behind his closest title rival Lando Norris, who started 15th for McLaren and ended up fourth with a bonus point for fastest lap.
"That was probably the most stressful afternoon in my life," said Piastri, after soaking up relentless pressure from Leclerc to take his second career win.
"It definitely goes down as one of the better races of my career."
Triple champion Verstappen's lead over Norris, who went long on his first stint with hard tyres and then reeled in a 15 second gap to pass the Dutch driver on lap 49 thanks to his fresher mediums, was cut from 62 points to 59.
McLaren are now 20 points clear of Red Bull in the standings with seven grands prix and three sprint races remaining.
Leclerc led away from the start, and was six seconds clear before the first pitstops, but his fourth successive pole in Baku was destined to end in defeat like the previous three.
Piastri was first of the two to change tyres, on lap 16 with Norris crucially preventing Perez from getting an advantage from an earlier stop, and Leclerc followed him down the pit lane a lap later.
The Australian then made a great passing move on Leclerc at turn one but the battle was just heating up.
The top two duelled for lap after lap, with the Ferrari driver trying in vain to use the DRS drag reduction to get past Piastri who had the benefit of clean air as he defended tenaciously but fairly.
Time and again Leclerc tried but just fell short on a circuit with long and fast straights along the shores of the Caspian Sea but tight and twisty turns through the old town.
"I couldn't get as close as I wanted and eventually we lost the race when I didn't defend as well as I could have at the end of the straight," said Leclerc. "Sometimes you make mistakes and I'll learn from it."
Big shunt
Piastri eventually pulled clear to win by 10.910 seconds while Leclerc fell into the clutches of Perez and Sainz, with a dramatic sting in the tail.
Perez tried and failed to pass Leclerc on the penultimate lap, opening the door for Sainz to breeze past the Mexican and into third at turn one.
As the Red Bull driver tried to get the place back at turn two, the cars collided and went into the wall in a shunt that effectively ended the race with the virtual safety car deployed to the finish.
"What happened there?," exclaimed Sainz, with Perez asking the same thing in more colourful language. Both were summoned to the stewards, who decided to take no further action.
Fernando Alonso was sixth for Aston Martin with Williams benefiting from the late crash to see Alex Albon finish seventh with Argentine rookie Franco Colapinto following him home in eighth.
The points windfall catapulted Williams above Renault-owned Alpine and into eighth place overall.
Seven times world champion Lewis Hamilton was ninth for Mercedes, after starting in the pit lane due to a power unit change, and British rookie Oliver Bearman collected the final point for Haas as a stand-in for suspended Kevin Magnussen.
Bearman has now scored points for two different teams in his two races after making his race debut with Ferrari as a stand-in for appendicitis-stricken Sainz in March.