The win leaves the Pramac rider 21 points ahead of his Italian Ducati rival, who finished third behind Spaniard Pedro Acosta in sweltering conditions at the Mandalika track on Lombok island.
In front of a crowd of 60,000 in motorbike-mad Indonesia, he put his tumble in Saturday's sprint behind him, cancelling out the gains his title rival Bagnaia made after securing victory in that race.
"Thank you Indonesia. I am very happy. This is not just a victory. After what happened last season, after yesterday's crash, to perform this way was quite difficult," Martin said.
"I trust in myself but I thought I would crash at every corner. There was a lot of pressure from Pedro so I am really happy."
There was an intense all-Italian battle for the final podium place between Bagnaia, Enea Bastianini, Franco Morbidelli and Marco Bezzecchi.
But Bagnaia, who won at Mandalika last year, nabbed it after his teammate Bastianini crashed with six laps remaining and he overtook Morbidelli.
"A very, very tough race. I struggled a lot at the start. It's the fifth start in a row that we are struggle. We have to improve that area," Bagnaia told reporters.
"As soon as I was back at the front I was able to push, but it was too late."
But as riders crossed the finish line, it was announced Acosta was being investigated for a tyre pressure infringement.
A potential penalty would give Bagnaia a glimmer of hope of second and four more points, limiting Martin's lead to just 17.
Crash-filled race
More than half a dozen riders crashed out of the race.
Marc Marquez's outside title chances potentially went up in smoke after he drove off the track with flames flickering from his bike.
Aleix Espargaro, Alex Marquez, Jack Miller and Luca Marini all retired after a pile-up on the first lap. Fabio Di Giannantonio and Joan Mir also crashed out.
Aprilia's Miguel Oliveira was ruled out of the race weekend and left a doubt for next week's Japan MotoGP after fracturing his right wrist in Friday morning's free practice session.
Ducati have already sealed the constructors' championship.
MotoGP returned to Indonesia in 2022 for the first time since 1997, at a new venue on the resort island of Lombok.