Ferrari's Sebastian Vettel won a hectic and thrilling Bahrain Grand Prix as Lewis Hamilton's hopes were hit by a penalty for gamesmanship.
Mercedes' Hamilton was penalised five seconds for driving slowly on pit entry to hold up Red Bull's Daniel Ricciardo.
Serving it at his second and final pit stop, Hamilton rejoined in third place.
Team-mate Valtteri Bottas was ordered to let Hamilton by so he could chase Vettel, but 12 seconds in nine laps was too big a task and he took second.
Vettel's win gives him a seven-point lead over Hamilton in the championship.
A major battle on the horizon
It was not completely clear who had the quickest car - Ferrari argued it was them; Mercedes boss Toto Wolff said Hamilton was the fastest. And the world champions may well feel this was a race that got away from them.
But the best race of the season, in which all three so far have been good, underlined one key fact - Ferrari are absolutely competitive and they and Mercedes are in a titanic struggle for the championship.
Hamilton and Vettel look like the men who will fight it - Bottas is not yet on Hamilton's level and Kimi Raikkonen was again relatively anonymous, a poor start leaving him a long way behind but recovering to take fourth place ahead of Ricciardo.
Red Bull's Max Verstappen may well have been in the fight, too, but crashed out after 11 laps with brake failure.
How did Vettel beat Hamilton?
It turned on two key moments - an aggressive early pit stop by Ferrari for Vettel; and Hamilton's decision to slow on the way into the pits when the safety car was sent out following a collision between Williams driver Lance Stroll and Toro Rosso's Carlos Sainz.
But other issues played a part, too - Hamilton should probably have taken pole but made a mistake at Turn 10 on his final qualifying lap, allowing Bottas to do so for the first time in his career.
And Mercedes, in a very difficult position in only the third race of the year, waited until just short of half-distance to order the slower Bottas to let Hamilton by for the first time.
The combination of those two things left Hamilton with too much to do on a frustrating afternoon on which he and Mercedes were arguably the fastest combination but victory slipped through their fingers.
The race that kept everyone guessing
Hamilton's decision to slow in front of Ricciardo on the way into the pits on lap 13 was made because he was behind Bottas, who was also coming in, and he wanted to give himself time to have his tyres changed straight after his team-mate and not lose a place to the Australian.
He apologised to the team afterwards for his actions, which he said might have cost him victory.
A slow pit stop for Bottas made matters even worse for Mercedes.
Ferrari's aggressive strategy was triggered when Bottas converted his first career pole, in his third race for Mercedes, into a first-lap lead but did not have the pace to get away from his pursuers.
Team boss Wolff said Bottas had too-high tyre pressures as a result of a problem on the grid.
The Finn found himself leading a five-car train, comprised of Vettel, Hamilton and the Red Bulls of Verstappen and Ricciardo.
Unable to pass, but clearly being held up, Ferrari chose a very early first pit stop for Vettel on lap 10, switching onto a second set of the super-soft tyres. Verstappen followed him in a lap later but suffered brake failure at the last corner immediately afterwards.
Hamilton closed in on Bottas, only for the safety car to be introduced on lap 13 after the crash at the first corner.
All the remaining front runners pitted - Hamilton earning his penalty - and Bottas fitted the super-softs and Hamilton the more durable softs.
At the re-start four laps later, Vettel led Bottas and Hamilton and the Ferrari started to pull away, and as he built a lead of more than four seconds by lap 25, Mercedes finally ordered Bottas to let Hamilton through into second place.
Hamilton closed his deficit to Vettel from 6.3 seconds on lap 27 to 4.3 on 32, and the Ferrari pitted for the final time a lap later, rejoining 17 seconds behind on soft tyres Hamilton but immediately setting fastest laps.
Hamilton served his penalty at his final pit stop on lap 41, fitting another set of soft tyres and rejoining 18 seconds behind the leader, and now it was the Englishman's turn to set fastest laps.
Bottas moved out of the way into Turn 13 on lap 47, leaving Hamilton with 12 seconds to close in 10 laps. It was too much.
A gripping race was enlivened by battles throughout the field, one of the most intense of which was between Renault's Jolyon Palmer, Toro Rosso's Daniil Kvyat and McLaren's Fernando Alonso.
They swapped positions for many laps, racing furiously, but Alonso's frustration at battling with his down-on-power Honda engine grew throughout.
He said he had "never raced with less power in my life" and later, after losing out to Kvyat but finally moving clear of Palmer, he found Esteban Ocon's Mercedes-powered Force India coming up behind him and blasting past on the straight.
"He was, what, 300 metres behind us on the straight?" Alonso said to his engineer Mark Temple. "We're considering Plan B," Temple said. "How are the tyres?" Alonso replied: "Do what you want, man."
Alonso eventually retired with an engine problem, the latest in a series for Honda in a weekend in which they have lost four MGU-Hs, one of them on team-mate Stoffel Vandoorne's car before the race even started.
Driver of the day
What they said
Vettel: "It was a really great day. It was the last half of the out lap when all the fireworks were going off I was like: 'I love what I do'.
"It was a great team effort today and I felt like we are quick. I tried to put Valtteri under pressure. But the early pit stop worked."
Hamilton: "Congratulations to Seb. The pitlane was my fault and apologies to the team. I tried my hardest to catch up. We will push hard together, keep fighting.
"Losing points for the team is definitely painful but it is what it is. I am getting old!"
What happens next?
Drivers have a two-week break before the Russian Grand Prix around the Olympic Park in Sochi on 28-30 April. Hamilton has won twice there. Vettel, on the other hand, crashed last year after being hit twice by Daniil Kvyat.