The Spaniard looked dead and buried when he missed his turn-in point for Turn 6 on his first flying lap of the Losail circuit and fell in the gravel on the outside of the corner.
But after remounting his Pons Kalex and stopping in the pits for checks, Espargaro headed back out and occupied seventh spot by the mid-way point of the session.
He seemed set for a second-row starting spot as he broke the timing beam in sector three of his last lap 0.4s adrift of Takaagi Nakagami's previous benchmark.
But, aided by a tow down the start/finish straight from Johan Zarco, Espargaro crossed the line in 2m00.882s to knock the Japanese rider off pole by just over a tenth of a second.
It was Espargaro's ninth pole in Moto2 and equalled Italtrans rider Nakagami's career-best too.
A late flyer from Mika Kallio snuck the Marc VDS rider in ahead of his British team-mate Scott Redding - who had led the session for the opening 30 minutes.
Dominique Aegerter was another to improve late on. The Swiss rider will start fifth ahead of Julian Simon and Nico Terol, who ran into the gravel immediately after setting his best time with two minutes remaining.
Among the other crashers were Moto3 world champion Sandro Cortese, who is making his Moto2 debut this weekend, Axel Pons, Mattia Pasini and Sergio Gadea.
- Sports & Recreation