Cal Crutchlow claimed third, meaning Jorge Lorenzo - who had been top as the chequered flag flew - had to settle for fourth ahead of Dani Pedrosa and Alvaro Bautista.
A sudden downpour right at the start of Q2 threw preparations into chaos, although its equally sudden finish – and the high temperatures of Malaysia – meant all the riders were able to brave slicks for the pole shootout.
Lorenzo still found the track treacherous, having to abandon his first run after a big moment and save at Turn 7.
In those conditions his Yamaha team-mate Rossi flew, with his first effort a full three seconds better than the rest of the field.
The track continued to dry however, meaning timing was crucial as riders found massive gains on their final runs.
Lorenzo kicked it off, jumping to top spot before improving to a 2m00.578s as the chequered flag flew.
A host of riders were on quicker laps however, including Honda's Marquez and Pedrosa – who at the time were languishing down in eighth and sixth respectively.
Crutchlow and then Rossi duly demoted Lorenzo, meaning that Rossi stood on the brink of his 50th grand prix pole.
That situated lasted all of five seconds: Marquez came across in a record 2m00.011s to seal pole by three tenths of a second.
Pedrosa's lap unravelled and meant he had to settle for fifth, having set the pace in every session leading up to qualifying.
Bradley Smith, Andrea Dovizioso and Pol Espargaro – who progressed through from Q1 alongside Andrea Iannone – will share the third row.
Stefan Bradl did not compete in the session, and will also miss Sunday's race, after fracturing his right ankle in fourth practice.
The German fell at Turn 1 and appeared to catch his ankle in the astroturf run-off.