In humid conditions at the Mandalika circuit, Ducati-Pramac's Martin sped around in one minute and 29.088 seconds, smashing the record set just a day before by more than half a second.
"I am so happy. Today I felt great. It went perfectly so this is always nice," Martin told reporters.
He was 0.535 seconds ahead of Marco Bezzecchi, who survived a crash to secure a front-row place on his final lap. Red Bull GasGaS's Pedro Acosta was third, a further 0.048 seconds behind.
"I lost a bit of concentration and I crashed. Fortunately, the bike was okay," said Ducati-VR46 rider Bezzecchi.
"I knew I could be fast but I didn't imagine the first row so what can I say? I am very happy."
Defending champion Bagnaia and Enea Bastianini will start on the second row after finishing fourth and fifth.
Marc Marquez, fourth in the championship standings, slid out twice in the second qualifying session and will begin from 12th after failing to register a lap.
Defending champion Bagnaia trails Martin by 24 points with six race weekends left with a maximum of 37 points available on each.
The Italian Ducati rider lost ground on the Spaniard after crashing out in the Emilia-Romagna MotoGP last week.
Bagnaia's teammate Bastianini and Gresini's Marquez are 59 and 60 points behind Martin, still with an outside chance of edging their way back into the title picture.
Bastianini had set the new Mandalika track record in practice on Friday at 1:29.630 before Martin's spectacular qualifying lap.
MotoGP returned to Indonesia in 2022 for the first time since 1997, at a new venue on the resort island of Lombok.