The Spaniard clocked one minute 54.634 seconds to edge out his Honda-powered rival by 0.221 seconds after the lead shifted back and forth between the pair.
Stoner was quickest in the practice part of the session and made a bid for pole 11 minutes from the session's conclusion with a lap of 1:55.149.
Lorenzo responded by breaking 1:55 for the first time but Stoner nudged back ahead with 1:54.855, only for Lorenzo to have the final say.
Cal Crutchlow completed the front row for Monster Yamaha Tech 3, the Englishman's best ever qualifying result.
Yamaha's Ben Spies was fourth despite a late crash, holding off Ducati's Nicky Hayden and Crutchlow's team-mate Andrea Dovizioso.
The Hondas of Dani Pedrosa and rookie Stefan Bradl sandwich Pramac Racing's Hector Barbera on the third row, while Ducati's Valentino Rossi could only manage 12th place behind Karel Abraham and Alvaro Bautista.
Colin Edwards, Randy de Puniet, who crashed 20 minutes from the end but escaped injury, and Aleix Espargaro make up row five.
Danilo Petrucci, Ivan Silva, Mattia Pasini and James Ellison also all walked away from crashes but, like De Puniet, will have to assess any damage to their bikes.