Misano Adriatico - Spaniard Maverick Vinales left it late before grabbing pole position for the San Marino MotoGP on Saturday ahead of Italy's world championship leader Andrea Dovizioso and Spanish title-holder Marc Marquez.
With just four seconds left in qualifying for Sunday's race at Misano the 22-year-old Yamaha rider crossed the line in a fastest final lap of 1min 32.439sec, upsetting local favourite Dovizioso.
Ducati's Dovizioso, 31, and defending world champion Marquez, who was unhurt after crashing on his final lap, will complete the front row.
"Today the bike was working really good," said Vinales. "So I knew if I was riding well I could take pole.
"The bike is improving race by race and I'm happy we still have room to improve. Let's hope it doesn't rain. But rain or no rain we have to be strong and competitive.
"We are four riders quite close so it could be a group at the beginning and whoever chooses the tyre right it could make the difference."
In a three-way battle, Dovizioso was on provisional pole as Vinales and Marquez began their final pushes, with the young Spaniard going fastest after Marquez fell out of contention.
"It's by far my best qualification ever in Misano," said Dovizioso, a native of Forlimpopoli, a town just 65 kilometres (40 miles) from the Misano circuit in Rimini.
"In my opinion we are well-placed for the race. We'll wait to see what will come tomorrow: in the wet everything changes."
Rain is forecast for Sunday when Yahama's hopes are focused on Vinales after Italian teammate Valentino Rossi, fourth in the world standings, broke his leg in a training accident.
Vinales, sitting third overall behind Dovizioso and Marquez, claimed a fourth pole position of the season as he aims to follow wins in Qatar, Argentina and France.
Marquez, winner in San Marino four times in all categories, blamed a change in tyres for his tumble.
"Happy with the pace although we expect rain tomorrow," Marquez later tweeted.
"I did a good first lap, then I wanted to try the other tyres," explained the reigning world champion who lost his overall lead to Dovizioso last time out in Silverstone.
"I made a small mistake straight away and after that another, more serious, going off onto the white line: (let's have) patience, my placing is still good, I'm happy."
Britain's Cal Crutchlow (LCR Honda), riding with a finger injury, heads the second row after narrowly beating Jorge Lorenzo to fourth, with Tech 3 Yamaha's Johann Zarco completing the top six.
Spaniard Dani Pedrosa clocked the seventh fastest time, one place higher than last year when he went on to win.
Italians Mattia Pasini (Kalex) and Enea Bastianini (Honda) took pole position in the Moto2 and Moto3 categories respectively.