Dani Pedrosa produced a sizzling record lap for pole at Sepang as Valentino Rossi stunned MotoGP title rival Jorge Lorenzo in the final seconds to secure a front row start in Malaysia.
Lorenzo believed he had qualified in third after the final time attack in the blistering heat but was left embarrassed as he attempted to enter parc ferme at the end of Q2, with Rossi pouncing for third with a stunning final lap to deny his Movistar team-mate by 0.011s.
World championship leader Rossi joins Repsol Honda pair Pedrosa and Marc Marquez on the front row for the crucial penultimate round on Sunday, with Lorenzo heading up row two along with LCR Honda's Cal Crutchlow and Andrea Iannone, who was a somewhat surprising sixth fastest on the Ducati GP15.
An electric final qualifying session was lit up by Rossi's last-gasp front row shock, but Pedrosa's incredible record lap deserves equal billing as the Honda rider – back in form since Aragon – lapped in 1m 59.053s to finish a huge four tenths of a second quicker than team-mate Marquez.
In a fascinating qualifying shootout, Marquez – accused by Rossi of favouring Lorenzo for the premier class title – clearly allowed the Majorcan to follow him during the first time attack as the outgoing world champion hit the top of the times with a lap in 1m 59.606s to lead the way from Lorenzo.
However, Pedrosa blitzed the previous record with a scorching lap on the RCV to take provisional pole and remained there until the chequered flag.
Rossi, holding a lead of 11 points over Lorenzo with two more races remaining, was 1.2s adrift after his first time attack in fourth behind Iannone.
With the leading riders operating a two-stop strategy, Marquez ventured out for his second flying lap but was unable to improve after losing the front at turn 12 and somehow managing to save himself from what looked like a certain crash, picking his Honda up on his elbow and burning a hole in his leathers as a result.
Iannone appeared to wait on fellow Italian Rossi at the end of pit lane as the pair went back out following their first stop and was happy for the nine-times world champion to follow him, although Rossi was unable to improve his position, lapping in 1m 59.810s to remain fourth.
Pedrosa's pole time seemed assured and no-one could get close in the final minutes, with Lorenzo finding little on his third time attack on the YZR-M1, 0.8s behind the Honda.
Marquez had no answer either as he settled into second place but there was one final twist to come when Rossi found a tenth in the final sector on his last lap to bump Lorenzo off the front row (1m 59.726s), striking a psychological blow in the dying seconds and forcing his rival out of the top three for the first time since Assen.
British rider Crutchlow lapped in 2m 00.199s for fifth ahead of Iannone, while Andrea Dovizioso put the second of the factory GP15s onto the third row in seventh, where the Italian lines up with Suzuki's Maverick Vinales and Bradley Smith on the Monster Tech 3 Yamaha.
Avintia Racing's Hector Barbera was an excellent 10th head of Aleix Espargaro, who was a faller in Q2, and Pol Espargaro on the second Tech 3 Yamaha.
Vinales and Smith progressed to Q2 after heading the times in Q1 respectively.
1 Dani Pedrosa
2 Marc Marquez
3 Valentino Rossi
4 Jorge Lorenzo
5 Cal Crutchlow
6 Andrea Iannone
7 Andrea Dovizioso
8 Maverick Viñales
9 Bradley Smith
10 Hector Barbera
11 Aleix Espargaro
12 Pol Espargaro