Pramac Ducati rider Martin threatened to take a 47-point lead in the championship as he led the field on the 11th of 25 laps ahead of Bagnaia and Marquez.
But a crash at Turn 6 flipped the grand prix on its head, with Bagnaia released into the lead and forced into a thrilling scrap with Gresini Ducati’s Marquez for the win.
Beating Marquez by 0.372s after putting in a stunning Jerez race lap record of 1m37.449s on the 23rd tour, Bagnaia is now just 17 points behind Martin having crashed out of Saturday’s sprint.
Marquez celebrated a first grand prix podium as a Ducati rider and his first since last year’s Japanese GP, while VR46 Ducati rider Marco Bezzecchi got his first rostrum of the campaign in third.
Converting pole to the holeshot, Marquez led the field through the first few corners as Martin slotted into second ahead of Bezzecchi and Bagnaia, who started seventh.
Signalling his intent from the outset, Bagnaia staged an incredible double overtake on the brakes on the outside of Bezzecchi and Martin into the Turn 6 hairpin to move up to second.
At Turn 13 at the end of the opening lap, Bagnaia scythed through Marquez when the door opened slightly in the middle of the corner.
But Marquez moved to retaliate instantly, attempting to retake the lead into Turn 1 on lap two but forcing both to go wide and allowing Martin into second as Bagnaia narrowly held onto first.
A mistake for Bagnaia at the final corner allowed Martin to come through to the lead and dropped the factory Ducati team rider to third, though only briefly as he outdragged Marquez to get second back as they began lap three.
Marquez was overtaken by Bezzecchi on lap four, while the top two – headed by Martin – streaked six tenths clear and then to almost a second come the start of the ninth tour.
What was shaping up to be a duel for the win between the two GP24s of Martin and Bagnaia ultimately didn’t materialise as the former lost the front end of his bike under braking for Turn 6 on lap 11.
This released Bagnaia into a lead of eight tenths as that also became the gap separating Bezzecchi and Marquez.
But Marquez soon upped his pace and moved through on Bezzecchi in a battle on the brakes at Turn 6 on lap 14.
This gave Bagnaia a 1.2s lead over Marquez, but would be chipped away by the Gresini rider as the pair traded fastest race laps.
At the start of lap 20, Bagnaia was now only half a second clear of Marquez and on the next tour the Gresini rider launched his GP23 up the inside of the Italian at Turn 9.
Bagnaia forced the issue into Turn 10 and the pair made contact, though the factory Ducati rider was able to take the lead back through Turn 11.
Marquez tried the same move on the next tour but Bagnaia had already anticipated it and ran defensively into Turn 9 to ensure his rival ran wide on the exit.
Bagnaia made a decisive break on lap 23 when he fired in the fastest lap of the race, which put his lead up to half a second and out of reach of Marquez.
Marking his second grand prix win of the season, Bagnaia is now just 17 points adrift of Martin in the championship.
Bezzecchi ultimately ended up 3.5s behind Marquez in third, with Alex Marquez fourth on the sister Gresini Ducati ahead of the second factory team Ducati of Enea Bastianini.
Brad Binder was the top KTM in sixth from VR46’s Fabio Di Giannantonio, Trackhouse Racing’s Miguel Oliveira, Aprilia’s Maverick Vinales and Tech3 rookie Pedro Acosta – who survived early contact with LCR Honda’s Johann Zarco.
The final points went to Raul Fernandez (Trackhouse Racing), Joan Mir (Honda), Alex Rins (Yamaha), Takaaki Nakagami (LCR Honda) and Fabio Quartararo (Yamaha).
Jack Miller (KTM) and Franco Morbidelli (Pramac) collided late on at Turn 5 to join the considerable list of retirees, with the stewards set to investigate the incident after the race.
Aleix Espargaro (Aprilia) and Johann Zarco (LCR) also collided at Turn 5 during the race, which is another incident under investigation.
KTM wildcard Dani Pedrosa fell off his bike on lap four, while Augusto Fernandez (Tech3) and Lorenzo Savadori (Aprilia) retired in the pits.