Ferrari's Sebastian Vettel beat Mercedes' Lewis Hamilton at the Azerbaijan Grand Prix to take his third consecutive pole position.
The German, nine points clear of Hamilton in the championship, was just 0.179 seconds clear of his rival around the demanding Baku street circuit.
Kimi Raikkonen was on course to snatch pole from his team-mate only to make a mistake at the final corner of his lap.
The Finn was sixth, behind Valtteri Bottas' Mercedes and the Red Bulls.
- Azerbaijan GP qualifying results
- Predict who will top the podium in Baku
- Chequered Flag podcast: Baku preview
- Whacky race: will we get another Baku epic?
Ferrari win three-way battle for pole
Vettel did all his work on his first run of the top-10 shootout, when he was 0.342secs quicker than Hamilton.
The world champion closed up on his final lap but it was not enough, meaning a lock-up at Turn Three by Vettel on his final lap was irrelevant.
Mercedes, Ferrari and Red Bull have showed similar pace through the practice sessions and it was hard to work out who would take top spot.
Ferrari have won twice already this season, with Red Bull taking victory at the last race in China. Mercedes are yet to win.
Raikkonen the weak link
Raikkonen will be kicking himself for losing what appeared to be his best chance of pole since Monaco last year.
A series of errors not only cost him top spot but also compromised his race strategy.
Following a mistake in the second session - which means he must begin the race on a less durable tyre compound - Raikkonen made another in the top-10 shootout at Turn 16, straddling the kerb and coming close to hitting the wall on the exit.
He tried again, and was on course to beat Vettel's time, but made yet another mistake - again at Turn 16 - where he had a huge slide and then 'tank-slapper', which affected his speed all the way up the long straight to the line.
He ended up 0.992secs off pole and will start sixth, behind the Red Bulls - with Daniel Ricciardo just pipping by Max Verstappen by 0.083secs - and on a worse tyre.
The Red Bulls were also very close to Bottas, with Ricciardo just 0.074secs off.
Huge crash narrowly avoided
The street circuit in Baku is one of the narrowest and fastest tracks on the F1 calendar, and there was a dramatic escape for the Toro Rosso drivers, when a misunderstanding led to Pierre Gasly almost running into the back of team-mate Brendon Hartley on the 180mph run through the kinks before Turn 15.
Gasly was on a flying lap and Hartley was in the middle of the track with a puncture. The New Zealander tried to get out of the way, going one way and then the other, but the Frenchman clipped him as he went past, fortunately only lightly in what could have been a horrendous accident had he been a few centimetres further to the left.
Gasly swore over the radio to the team saying it was "unacceptable". Hartley apologised, saying: "I was trying to get out of the way and I went the wrong way. I feel pretty stupid."
Feel the force
The Force Indias were best of the rest, Esteban Ocon edging Sergio Perez by 0.024secs and more than 0.5secs quicker than Renault's Nico Hulkenberg, who was 0.285secs ahead of team-mate Carlos Sainz the last of the top 10.
Hulkenberg has a five-place penalty for an unauthorised gearbox change and will start 14th, behind McLaren's Fernando Alonso, who qualified 13th fastest for the third consecutive race, and rookie Charles Leclerc, impressive in the Sauber after a difficult start to his F1 career.
Alonso's team-mate Stoffel Vandoorne was knocked out in Q1, nearly half a second slower than the Spaniard.
Williams had their best qualifying of the season, with Lance Stroll and Sergey Sirotkin 11th and 12th ahead of Alonso.
What they said
Hamilton, who has not won a race since the US Grand Prix last October, said: "That was close. We did the best job we could. It was very close.
"They have been quick all weekend. We are in the mix. This is the fifth best track to overtake and I am going to try to give Sebastian a hard time."
Vettel said: "I felt the car was good in Q1, Q2 and then in the last section I knew I had the car that does what I want.
"The first lap I was quite happy but there were maybe two parts where it was not exactly perfect.
"I knew the track would ramp up and I went out to go quicker. I was little bit up but then I locked up into Turn Three. I am a little upset with that because I thought I had a bit more but the important thing is we got pole.
"It will be an intense race. Anything can happen. Safety cars are likely. The important thing is the car is in a good place."
Raikkonen said: "I took it easily on the entry to make sure and just got some really odd snap and lost it and didn't spin but I knew once I got snap it was gone. I had a very good lap and it would have been the best possible outcome but small mistake and then you have that huge straight and it's gone."