Pons rider Espargaro required medical clearance to participate in qualifying after being collected by Johann Zarco in a collision at the first corner in final practice.
Although Espargaro showed no ill effects in qualifying, setting a rapid pace throughout, Marquez looked certain to start on pole for the seventh time in nine rounds. The Monlau rider, now confirmed as a member of Honda's works MotoGP line-up for 2013, was barely headed until the closing moments, when Espargaro burst through the traffic to register a 1m52.369s and grab the top spot away.
Zarco was given a 15-place grid penalty for his incident with Espargaro - and ironically then went on to set the third-fastest time in qualifying, which would have been a career-best for the Frenchman had it stood.
The JIR rider's demotion moves top local rider Andrea Iannone up to third for Speed Master.
Forward's Alex de Angelis and Paddock GP's Thomas Luthi were next up, with Bradley Smith going sixth quickest for Tech 3 before crashing at the end.
Smith was one of several riders to have incidents, along with his team-mate Xavier Simeon, Ioda rider Simone Corsi, Marc VDS Racing's Mika Kallio, MZ convert Mike di Meglio and Gresini's Ratthapark Wilairot. All escaped unhurt, although Simeon was in clear discomfort after landing heavily on his back.