The factory Aprilia rider put in the first-ever 1m28s lap at Circuit Ricardo Tormo with a 1m28.931s to take his first pole since Assen in 2021.
He led from factory Ducati rider Bagnaia, who dominated Q1 before finishing Q2 in second, while title rival Jorge Martin finished just sixth for Pramac.
Pramac’s Johann Zarco will line up third in his final race with the Ducati satellite team before moving to LCR Honda next season.
It hands Bagnaia the advantage heading into this afternoon’s sprint race, where he can clinch his second consecutive title if he outscores Martin by four points.
Martin narrowly escaped disaster at the start of Q2 after Marc Marquez’s tear-off nearly got caught in his airbox.
Vinales took an early lead with a 1m29.534s after the first laps, before he was bettered by factory KTM rider Brad Binder, with Martin second and Bagnaia last.
Bagnaia stayed out while the rest of the field pitted at the halfway mark, with Martin opting for a bike swap.
The reigning champion then went quickest with a 1m29.167s, ahead of Binder and Martin, before Bagnaia improved again to set a new lap record, with a 1m29.023s.
Marquez and Martin tussled at Turn 8, coming very close to making contact, as the Honda rider took added risks in his final weekend with the team before moving to Gresini for 2024.
Vinales then set his best time, a 1m28.931s, before Marquez crashed close behind Martin at Turn 2, the Pramac rider then backing out of his lap after a big wobble exiting Turn 4.
Alex Marquez (Gresini) then crashed, as did Binder at Turn 8.
Bagnaia put in a dominant performance in Q1, topping the session early on ahead of Alex Marquez, with his first effort quicker than any of his Friday laps.
Though Marquez put in. several quicker laps, Bagnaia then set a new lap record with a 1m29.054s enough to top the session, and he pitted with 90 seconds remaining.
KTM’s Jack Miller will line up fourth ahead of Binder, with Martin sixth.
VR46 Ducati rider Marco Bezzecchi was seventh ahead of the Marquez brothers, with Alex in eighth and Marc ninth.
Raul Fernandez finished 10th for RNF Aprilia, with Qatar GP winner Fabio Di Giannantonio ahead of Aprilia’s Aleix Espargaro to round off the top 12.
Augusto Fernandez narrowly missed out on a Q2 spot on his Tech3 GasGas machine, continuing his strong rookie season, with Enea Bastianini behind him on the factory Ducati and factory Yamaha rider Fabio Quartararo completing the fifth row.
LCR’s Takaaki Nakagami, VR46’s Luca Marini and Pol Espargaro (GasGas) make up row six, with Franco Morbidelli (Yamaha), LCR’s Alex Rins and Lorenzo Savadori, filling in for an injured Miguel Oliveira at RNF, completing the grid.