Lewis Hamilton headed a Mercedes one-two in qualifying at the Austrian Grand Prix despite both cars going off on their final laps of the session.
Hamilton did enough on his first lap in the top 10 shoot-out, lapping 0.2secs quicker than team-mate Nico Rosberg.
Hamilton then spun at Turn One on his final lap, leaving Rosberg the chance to beat him, only for the German to spin at the final corner on his lap.
Ferrari's Sebastian Vettel was third from Williams's Felipe Massa.
It was nip-and-tuck whether Rosberg would have beaten Hamilton - he was slower than the world champion in sector one, but quicker in sector two.
But then Rosberg ran wide on the exit of Turn Eight, got the car partly onto the kerb and then lost control as he went into the final corner.
"It was quite a difficult qualifying with the track conditions," said Hamilton. "I was grateful I got my third lap in on the first run in Q3. I was pushing that bit extra on the next run and just locked the rears.
"I wasn't too concerned about losing pole at that time because I was trying to get the car going, but obviously afterwards I was thinking: 'I probably lost it there.'"
Rosberg said: "Definitely gutted, but Lewis was 0.2secs up on the previous lap and I was 0.2secs exactly up at that point so I knew I had to go for it."
The mistakes from both Mercedes drivers gave Vettel the chance to beat them, but he could only close to within 0.355secs of Hamilton with his final lap.
Vettel's team-mate Kimi Raikkonen messed up his runs in the first part of qualifying, which was held on a damp track, and failed to progress.
The Finn qualified 18th, although he will be promoted four places on the grid because of penalties for the Red Bull and McLaren drivers, and uttered an expletive when asking over the radio: "How is that possible?"
Kimi Raikkonen was knocked out in the first session of qualifying
Hamilton's pole was his seventh in eight races this year.
Nico Hulkenberg, fresh from winning Le Mans with Porsche last weekend, was an excellent fifth for Force India ahead of the second Williams of Valtteri Bottas.
And there was another highly impressive performance from 17-year-old Max Verstappen, who put the Toro Rosso seventh after also starring in the damp first part of qualifying.
His team-mate Carlos Sainz was also strong in the wet, but as the circuit dried out he could manage only 13th.
The top 10 was completed by Red Bull's Daniill Kvyat, who has a 10-place grid penalty for an engine change, the Sauber of Felipe Nasr and Lotus's Romain Grosjean.
Austria was treated to a hare tackling the Spielberg kerbs
The second Red Bull of Daniel Ricciardo, who has the same penalty as Kvyat, was only 14th on the grid on a dark day for the team on their own track. The Australian will have to take a five-second penalty at a pit stop in the race because he cannot serve his full penalty.
It was even worse for McLaren, whose drivers collectively have 50 places of grid penalties.
Fernando Alonso got the McLaren-Honda and its major aerodynamic upgrade into second qualifying but could go no further and took 15th.
The double world champion has a 25-place penalty for changing parts of his engine and his gearbox and will have to take a drive-through penalty early in the race as part of the complicated rules for penalty positions unserved.
It was even worse for Alonso's team-mate Jenson Button. The 2009 world champion excelled in the damp conditions in first qualifying, considerably faster than Alonso, but he lost ground as the circuit dried out and ended up 17th.
As he has a 25-place grid penalty , that means Button will have to serve a 10-second stop-and-go penalty early in the race to make up for the part of the penalty he has not been able to serve.
Nico Rosberg walks away dejected after losing control on his flying lap