Jorge Martin, 21 points behind Bagnaia in second place in the world championship at the final round, was second quickest on the Pramac Ducati, 0.147s down on Viñales' 1:29.142s at the Ricardo Tormo circuit.
Viñales' time bettered the 2016 lap record held by Jorge Lorenzo (1:29.401s).
Bagnaia was only 15th and 0.659s off the pace on the Lenovo Ducati after an incident-filled afternoon practice.
The Italian was under pressure to break into the top 10 to seal his spot in Q2 and as he prepared to leave his pit box, title rival Martin was among several riders who were lining up behind the defending world champion.
Irked Ducati team boss Davide Tardozzi made his feelings clear as he stood in front of the Pramac Ducati pit box and stared directly at Martin, who forced Tardozzi to move out of his way as he set off behind Bagnaia.
Martin, who was visibly quicker than Bagnaia, then shadowed his rival on the out lap and remained locked onto the rear wheel of the factory Ducati rider, who was unable to pull a time out of the bag on his flying lap.
Bagnaia was then hampered by yellow flags after Pol Espargaro crashed out, preventing him from improving his time and breaking into the all-important top 10.
He will now have to participate in Q1 on Saturday, when he will be up against team-mate Enea Bastianini, Yamaha's Fabio Quartararo, Alex Marquez (Gresini Ducati) and VR46 Ducati's Luca Marini for one of the two Q2 places up for grabs.
Free Practice 1 pace-setter Johann Zarco was third-fastest for Pramac Ducati ahead of Fabio Di Giannantonio (Gresini Ducati), Marco Bezzecchi (VR46 Ducati) and Brad Binder (Red Bull KTM), who made significant progress in the afternoon on the factory RC16.
Marc Marquez, competing in his final weekend as a Repsol Honda rider before his much anticipated first test on the Gresini Ducati at Valencia on Tuesday, was seventh.
Miller made it through despite suffering a high-side at Turn 3, bringing the session to a halt with a red flag due to debris on the track with around 15 minutes to go.
A spate of crashes occurred earlier, with Aleix Espargaro caught out first at Turn 1 quickly followed by Bezzecchi, who suffered a heavy tumble at Turn 8.
Repsol Honda's Joan Mir crashed at the same fast corner as Bezzecchi and was ruled out of the session as a result.
Augusto Fernandez also went down on the GasGas Tech 3 KTM, appearing to lose the front of the satellite machine at Turn 14, while Alex Rins – making his final appearance for the LCR Honda team – slipped off at Turn 1.
Franco Morbidelli will also compete in Q1 after the Monster Energy Yamaha rider was 14th behind team-mate Quartararo.