Lewis Hamilton beat Mercedes team-mate Nico Rosberg in a private battle for pole at the Spanish Grand Prix.
Hamilton had looked to be struggling, but a superb lap in the final seconds gave him a fourth pole in five races.
Hamilton was 1.053secs quicker than Red Bull's Daniel Ricciardo in third.
Williams' Valtteri Bottas and Lotus' Romain Grosjean were fourth and fifth ahead of Ferrari's Kimi Raikkonen and Fernando Alonso, while Sebastian Vettel will start 15th on the grid.
The world champion finished 10th but was later given a five-place grid penalty for changing his gearbox for the race.
The German had originally failed to set a time in the top-10 shoot-out after his Red Bull lost drive coming out of the pits at the start of the session.
Finn Raikkonen was just 0.036secs ahead of team-mate Alonso as Ferrari slipped back in competitiveness following an upgrade in performance from Renault, engine supplier to Red Bull and Lotus.
McLaren's Jenson Button was eighth ahead of the Williams of Felipe Massa.
Hamilton had been in supreme form on Friday, but appeared to be lagging behind Rosberg through final practice and the first two parts of qualifying, in which the German was quicker.
But after the final session was briefly stopped to recover Vettel's Red Bull, which was stranded at Turn Three, Hamilton was nearly 0.3secs clear of Rosberg on their first runs.
Rosberg closed the gap slightly on their all-important final run, but Hamilton just had too much.
"It's been a tough day," Hamilton said. "Nico has been driving really well so I didn't know whether I'd be able to get it. But right at the end I managed to get absolutely everything out of the car.
"Yesterday I had such a great day and I was really happy with the car. Often when things are that good you don't want to change much, but we changed a couple of small things here and there.
"Today the track went down [in pace] and the car was a real handful. Lots of oversteer, very inconsistent corner to corner and once you're in qualifying there is nothing you can do.
"So that's why we made some changes. I was struggling to put a lap together but in the end I made it by the skin of my teeth."
Rosberg said he was "very disappointed", adding: "I don't enjoy coming second to Lewis. In the end it was a good lap from me, and Lewis just did a better job.
"That's the way it is. It's still all to play for tomorrow. It only takes a good start and I'm in the lead again."
Ricciardo said: "Best of the rest but it's not quite good enough. Still a second off. That's too much. We've made some improvements, but Mercedes have as well."
Grosjean's fifth place is comfortably Lotus' best qualifying performance of the year after a difficult start.
The team have been battling reliability problems after a late start to pre-season testing, but after making progress in the first four races are finally in a position to exploit the car's potential.
The Franco-Swiss's team-mate Pastor Maldonado qualified last after crashing at Turn Three on his first lap of the first session.