Pre-session favourite Aleix Espargaro, who whitewashed practice at the Losail track, crashed twice and wound up ninth overall.
The latter fall came one minute from the chequered flag, and while he was up on the provisional pole time - which at that point belonged to Bautista.
The Spaniard seemed set for only his second career pole, but as the chequered flag flew Marquez - riding with a broken leg - came round in 1m54.507s to snatch top spot by just over five hundredths of a second.
Bradley Smith will start from the outside of the front row, having also got within 0.1s of Marquez's pole time.
MotoGP 2014: The key storylines
Ducati's Andrea Dovizioso meanwhile shaded Factory riders Jorge Lorenzo and Dani Pedrosa in the fight for fourth.
Stefan Bradl and Cal Crutchlow will share row three with Espargaro, with Valentino Rossi therefore consigned to the fourth row alongside Andrea Iannone and Pol Espargaro.
The latter prevailed in QP1 alongside Cal Crutchlow, although both were made to sweat when Nicky Hayden threatened an upset in the final two minutes.
The Aspar Honda rider dipped under Crutchlow's best in the first sector, but ultimately fell a few tenths short of demoting the Briton.
Hayden will therefore start from 13th, ahead of compatriot Colin Edwards and team-mate Hiroshi Aoyama.
Scott Redding suffered a recurrence of earlier electrical gremlins but was able to secure 16th ahead of Pramac's Yonny Hernandez, who crashed heavily at Turn 2 in the opening minutes of the session.
- Sports & Recreation
- Aleix Espargaro
- Cal Crutchlow
- Andrea Dovizioso
- chequered flag