Setting a new lap record, this immediately fell to teammate Jack Miller just moments later, who fired in a 1m53.215s having set an unofficial lap record during the Qatar test a fortnight ago.
Fabio Quartararo raised the game further with eight minutes remaining, the factory Yamaha rider moving to provisional pole with a 1m53.215s while teammate Maverick Vinales completed the front row.
This remained the time to beat until the final minute when Bagnaia began lighting up the timing screens.
The Italian rider completed his lap in the first-ever sub-1m53s lap of the Losail circuit with a 1m52.772s to claim his first MotoGP pole on his factory Ducati debut.
Quartararo held onto second, while Vinales broke Petronas SRT’s Valentino Rossi’s heart at the death by jumping up to third with a 1m53.088s to complete the front row.
Rossi shadowed his protege Bagnaia on his record lap to produce a 1m53.114s, which puts him at the front of row two ahead of the sister works Ducati of Jack Miller and Pramac’s Johann Zarco – who broke the all-time MotoGP speed record in FP4 when he was clocked at 364.4km/h.
Zarco’s late improvement shuffled Franco Morbidelli down to seventh on his ‘A-spec’ SRT M1, with Aleix Espargaro on the Aprilia heading Suzuki’s Alex Rins.
Reigning world champion Mir could only muster 10th on his GSX-RR having come through Q1 alongside Takaaki Nakagami, who put his LCR Honda 11th.
Pol Espargaro will start his first race as a factory Honda rider from 12th.
Avintia rookie Enea Bastianini occupied second as Q1 wound down having briefly held top spot on his two-year-old Ducati.
But a late improvement for Nakagami demoted the reigning Moto2 world champion down to 13th on the grid, Bastianini missing a place in Q2 on his MotoGP debut by just 0.005s.
Fellow rookie Jorge Martin was also on course for place inside the top two on his final lap as he tailed Nakagami, but the Pramac rider crashed at Turn 12 and was resigned to 14th.
But Martin still claimed several big scalps in double race-winner Miguel Oliveira, who was KTM’s top rider in 15th, and double podium finisher Alex Marquez in 16th on the LCR Honda.
Marc Marquez’s factory Honda stand-in Stefan Bradl will start 17th ahead of the second Avintia rookie Luca Marini, while Brad Binder could do no better than 19th on his KTM.
Tech 3 duo Danilo Petrucci and Iker Lecuona, and Aprilia’s Lorenzo Savadori completed the 22-rider field.