Vinales took his first pole since 2021 with a time of 1:28.931 to pip Ducati's Bagnaia. They will be joined on the front row by Johann Zarco in his final race for Pramac Racing before joining LCR Honda from next season.
"We missed something during the rest of the season, unfortunately. We're understanding the setting and the balance of the bike," said Vinales, whose last pole came in the Dutch Grand Prix two years ago.
"In Qatar and over here, we've done a really good job."
Zarco's teammate Martin will start behind KTM duo Jack Miller and Brad Binder on the second row.
Bagnaia, who has six race victories this season, is on 427 points while Martin has 416 heading into Saturday's sprint, which could decide the title.
"It's fantastic, I'm very happy," said Bagnaia, who had to begin qualifying in Q1 after a poor practice run on Friday.
"Yesterday, we were a bit unlucky, we didn't have the time to try different settings, which we did today. This morning as soon as I started the session I felt better.
"I'm ready for this afternoon, for sure the objective was to be in the front row."
Six-times MotoGP champion Marc Marquez finished ninth-fastest, one behind brother Alex (Gresini).
Earlier on Saturday, Bagnaia finished fastest in Q1 with a time of 1:29.054.