Pramac’s Martin moved seven points clear of Bagnaia after a dominant sprint victory on Saturday and looked on course to inflict further damage on the world champion in Sunday’s contest.
Taking the lead from sixth on the grid off the line, Martin was three seconds out front when he slid off his Pramac Ducati at Turn 11 on lap 13 of 27.
While this initially promoted Aprilia’s Maverick Vinales to the lead, Bagnaia – who leaped from 13th to third in the first three laps – would ultimately overhaul the Spaniard to take the victory.
Vinales held onto second after reeling Bagnaia in over the last lap, with Fabio Quartararo completing the top three on his factory Yamaha in an attritional grand prix.
Bagnaia now leads the championship by 18 points heading to next week’s Australian GP and becomes the first rider in 17 years to win a dry GP from outside of the first four rows.
Martin used the Ducati’s new start device to maximum effect to go from sixth to first at the start, as Vinales and Quartararo slotted in behind him.
Bagnaia made a much more aggressive start to the grand prix than he did on the sprint, getting up to eighth in the opening turns.
His charge continued as he found himself in sixth at the end of lap one and fourth at the conclusion of the following tour.
On lap three Bagnaia took third away from Quartararo, who soon found himself behind Aprilia’s Aleix Espargaro and Brad Binder on the factory KTM.
Bagnaia was just under two seconds away from the leading duo when he moved up to third, as Martin began to twist the screws on Vinales.
The gap Martin had opened stood at 1.5s on lap five and was over two seconds come lap 10.
As he started lap 13, Martin – who was one of only a few riders to go for the soft front tyre – was three seconds clear of Vinales and appeared on course for back-to-back grand prix wins.
But Martin crashed going through Turn 11 moments later, releasing Vinales into the lead and flipping the championship picture on its head.
On the same lap, Quartararo moved back into third.
This came about as Binder had to serve two long lap penalties for separate incidents: the first was a lap one collision with poleman Luca Marini (VR46 Ducati), while the second was for a clash with RNF Aprilia’s Miguel Oliveira on lap 11.
Aleix Espargaro’s fading soft tyres then made him easy prey for Quartararo on lap 13 to move back into the podium places.
He duly hunted down the two riders ahead of him, with Bagnaia taking the lead from Vinales on lap 20.
While he was able to get his lead out to just over a second at one stage, he came under pressure over the final lap as Vinales and Quartararo closed in.
At the chequered flag, 0.433s covered the podium trio, with Bagnaia celebrating his sixth grand prix victory of the year and his first since the Austrian GP in August.
Gresini’s Fabio Di Giannantonio was a season-best fourth ahead of VR46’s Marco Bezzecchi, who remains in outside contention for the championship at 63 points adrift despite collarbone surgery just seven days ago.
Binder was sixth after his brace of long laps as KTM team-mate Jack Miller was seventh, while Ducati’s Enea Bastianini, LCR Honda’s Alex Rins in his first GP since Le Mans, and Aprilia’s Espargaro rounded out the top 10.
Takaaki Nakagami was 11th on the second LCR Honda ahead of Oliveira, Raul Fernandez (RNF) and Yamaha’s Franco Morbidelli, who rejoined after a trip to the pits early in the race.
Both Pramac riders registered DNFs as Johann Zarco crashed two laps after Martin, while neither Repsol Honda got to the chequered flag due to falls.
Tech3 duo Pol Espargaro and Augusto Fernandez suffered separate crashes, while Marini pulled out on lap six after his tangle with Binder.