Reigning world champion Casey Stoner again showed his mastery of wet conditions to take pole for Sunday's German MotoGP at Sachsenring.
The Australian had the last word in a frantic last 10 minutes of qualifying once the rain had relented.
Stefan Bradl, Dani Pedrosa, Ben Spies and Cal Crutchlow all exchanged quickest laps before joint world championship leader Stoner prevailed.
Spies and Pedrosa made up the front row with Britain's Crutchlow in fourth.
Local favourite Bradl had set the pace in atrocious conditions before the rain stopped with around 15 minutes left and the leading riders headed back out to improve on the earlier modest times.
Stoner later revealed he was forced to use his spare bike, which had a different set-up, but still managed to claim his fourth pole of the season.
"I was a little bit nervous going out there for the last 10 minutes because I didn't have the best set-up," said the Australian, who moved level with Jorge Lorenzo in the standings after winning last week's race in Assen.
"I am very happy because I didn't think we had done enough to be honest. I wasn't really happy with the way the bike was feeling in the turns but in the end we ended up with pole position."
Spies, who is not renowned for his love of wet conditons, was also pleasantly surprised after his on-board lap timer - which alerts riders to where they are gaining or losing time around the circuit - became disconnected.
"It was strange," he told BBC Sport. "We have struggled a lot in the rain, and particularly at this circuit, but I had a good feeling yesterday and again today.
"I was a bit lost in the last 10 minutes when my lap-timer quit working, so I was blind-folded really. I just kept trying to push and push and give it some stick, and I got on the front row."
The heavy rain earlier in the session caught out a number of riders, with Michele Pirro, Danilo Petrucci, Mattia Pasini and Hector Barbera all crashing, although none sustained serious injury.
Spaniard Marc Marquez took his sixth pole in eight races this season in the Moto2 class, while Sandro Cortese pipped British team-mate Danny Kent on his final lap to take his fourth pole of the season in Moto3.
Moto GP qualifying:
1. Casey Stoner (Aus) Repsol Honda 1:31:796
2. Ben Spies (USA) Yamaha + 0.193
3. Dani Pedrosa (Spa) Repsol Honda + 0.285
4. Cal Crutchlow (GB) Tech 3 Yamaha + 0.492
5. Jorge Lorenzo (Spa) Yamaha + 0.585
6. Stefan Bradl (LCR Honda) + 0.714
7. Nicky Hayden (USA) Ducati + 0.999
8. Andrea Dovizioso (Ita) Tech 3 Yamaha 1.409
9. Valentino Rossi (Ita) Ducati + 1.421
10. Aleix Espargaro (ESP/Aprilia) + 2.104
Moto 2 qualifying:
1 Marc Marquez (Spa) Suter 1:34.503
2 Julian Simon (Spa) FTR 1:34.548
3 Mika Kallio (Fin) Kalex 1:34.639
5 Gino Rea (Gbr) Moriwaki 1:34.908
9 Bradley Smith (Gbr) Tech 3 1:35.238
Moto 3 qualifying:
1 Sandro Cortese (Ger) KTM 1:42.989
2 Danny Kent (GB) KTM 1:44.474
3 Adrian Martin (Spa) Honda 1:44.532