Martin came through from the second row to secure a sixth victory in the last seven races as he continued to stake his claim for a maiden premier class title.
He now leads Bagnaia by seven points, who had to settle for eighth behind his Lenovo Ducati team-mate Enea Bastianini at the Mandalika circuit.
Bagnaia's task was made all the more difficult after he started from 13th on row five when he failed to make it through to Qualifying 2 in the morning.
VR46 Ducati's Luca Marini, back from injury after sustaining a broken collarbone in a crash at the Indian round at Buddh, claimed the runner-up spot, while team-mate Marco Bezzecchi, who broke his collarbone last weekend in a training accident, showed plenty of grit to secure a podium in third.
Marini took the lead off the line after earning a maiden premier class pole in qualifying with Maverick Viñales (Aprilia Racing) second and Yamaha's Fabio Quartararo coming through to third.
Viñales then passed Marini for the lead on the approach to Turn 2 with Martin fifth behind KTM's Brad Binder.
Martin made a move on Quartararo into Turn 1 at the start of the second lap but went wide, allowing the Frenchman back through.
Leader Viñales was half-a-second up on Marini, while Martin tried again to pass Quartararo for third and this time made it stick.
Aleix Espargaro was sixth after the Aprilia rider didn't make the best start from the front row. As he attempted to make up ground, the Spaniard lost the front when he attempted a pass on Binder's inside for fifth, wiping them both out at Turn 16.
Espargaro was placed under investigation, but no further action was taken.
Bagnaia was now ninth behind team-mate Bastianini on the third lap.
Viñales had increased his lead to 0.7s over Marini with Martin third. Quartararo, Fabio Di Giannantonio (Gresini Ducati) and the injured Bezzecchi were the top six.
Martin was on a charge and gained another position after a firm move on the inside of Marini for second.
Martin began to chase after Viñales, who was nine-tenths ahead, but the Pramac rider was able to slash the deficit and closed in on the leader.
Martin lined up a pass and hit the front for the first time on lap nine as Marini remained right in contention in third ahead of Bezzecchi.
Martin immediately began to open a gap and had pulled a cushion of 0.8s when Marini picked off Viñales for second on lap 10.
Bezzecchi now had Viñales in his sights as he battled for a podium.
The VR46 rider, who was only passed fit to race following FP1 on Friday after breaking his collarbone last weekend and undergoing surgery, took third on lap 11 but ran wide, allowing Viñales back through.
Bezzecchi again ran wide after making another pass on Viñales on the penultimate lap, losing some ground as he remained in fourth.
However, the Italian finally produced a decisive overtake on the 13th and final lap to deny Viñales a podium.
A big moment for Marini on lap 12 ended any chances he had of catching Martin, who went on to clinch his fourth straight Sprint victory by 1.131s from Marini. Bezzecchi was around 0.9s back in third.
Ducati has now wrapped up the Constructors' title with six rounds of the championship still left in 2023.
Quartararo finished fifth behind Marini while Di Giannantonio – without a ride in 2024 after Marc Marquez's deal to join his brother Alex at Gresini Ducati – was a solid sixth.
Bastianini held off a disappointed Bagnaia for seventh, with Aussie Jack Miller (Red Bull KTM) and Miguel Oliveira on the RNF Aprilia the top 10.
Joan Mir was 16th on the Repsol Honda behind Franco Morbidelli (Monster Energy Yamaha) while Binder finished 19th and last after re-joining the race when he was taken out by Espargaro.
Alex Rins, making his second comeback from injury after withdrawing from the Japanese Grand Prix two weeks ago, was 18th on the LCR Honda.