Mercedes' Valtteri Bottas took his first pole position by beating team-mate Lewis Hamilton by just 0.023 seconds at the Bahrain Grand Prix.
Hamilton was ahead by 0.052secs after the first laps in the top-10 shootout but appeared to have a scrappy final lap, allowing Bottas to edge ahead.
It was Mercedes' first front-row lock-out of 2017, with Ferrari's Sebastian Vettel third, 0.478secs off the pace.
Red Bull's Daniel Ricciardo took fourth ahead of Ferrari's Kimi Raikkonen.
Mercedes' special engine mode
Mercedes' advantage of nearly half a second was by far their biggest over Ferrari so far this season.
But an advantage in qualifying does not necessarily mean Mercedes' superiority will translate into the race on Sunday.
Mercedes have a qualifying engine mode that adds more extra power than their rivals, and Hamilton expects Ferrari to have an advantage on tyre usage in the heat.
For Bottas, who has replaced world champion Nico Rosberg following the German's retirement, it was his most convincing qualifying performance of the year, after trailing Hamilton by 0.2-0.3secs in the first two races.
Hamilton was quickest in the first two sessions and on their first runs in final qualifying, but was made to pay the price on his final lap for a slow middle sector, in which he lost 0.2secs, and then a mistake at the final corner, where he needed to correct a slide on entry.
How Bottas did it
It was an impressive performance by Bottas, especially after a dispiriting race in China last weekend where he spun behind the safety car and finished sixth.
The Finn appeared to adjust his approach to counter Hamilton's pace.
He had been quicker than Hamilton in the first two sectors of his first lap only for Hamilton to find extra pace in the third.
But, on his final lap, he took it easier in the first sector and then pushed harder in the next two and it made the difference.
What they said
Bottas, whose first pole came in his 82nd grand prix, said: "It took a few races but got it and hopefully it's the first of many.
"I just want to say a big thank you to the team. Both starting from the front row. We really focused on the evening conditions and got a lot of lap time out of the car.
"It's not an easy track to get anything right. It's quite technical. Easy to lock up and miss the apex. It is getting the lap together and it was a good enough balance for pole."
Hamilton said: "Big congratulations to Valtteri. He has been working so hard. Today he was just quicker, so hats off to him.
"The first lap was good. But it was so close. I was losing a bit of time in the first sector. The second lap was not as good. Just overall a little bit down. Overall, a great battle. That's how close qualifying should be. I'm generally happy with the job I did."
Vettel said: "I was generally very happy with how it went. We had some issues on Friday, just in terms of balance, Q2 was tight - just 0.05secs. I was very happy with my first lap but I was a bit down because 0.4secs was more than I expected. The last lap I tried a bit harder and it didn't work."
Ricciardo beats Verstappen in Red Bull scrap
Ricciardo benefited from a poor performance by Raikkonen, struggling with his dreaded understeer, to pip the Finn by just 0.022secs and take the final spot on the second row.
The Australian also beat team-mate Max Verstappen, who was sixth, for the first time this season in qualifying - the Dutchman blaming Williams driver Felipe Massa for blocking him.
Massa was eighth, behind Renault's Nico Hulkenberg, with Haas driver Romain Grosjean and Hulkenberg's team-mate Jolyon Palmer 10th and in the top 10 for the first time this season, albeit 1.2secs off the German.
Fernando Alonso, who has been the centre of attention this weekend after Wednesday's news he will compete in the Indianapolis 500 instead of the Monaco Grand Prix, was 15th.
The Spaniard said he was on course for about 12th or 13th on the grid with a lap 0.5secs quicker than in first qualifying but suffered an engine failure towards the end of the lap.
Team-mate Stoffel Vandoorne was 0.3secs slower than the Spaniard and did not make it out of first qualifying. Vandoorne's weekend has been plagued by engine problems - he lost two MGU-Hs, the part of the hybrid system that recovers energy from the turbo, on Friday.
Both men are likely to suffer engine penalties later in the season as a result of Honda's reliability issues.