The Italian Bastianini cruised home at Sepang more than 1.5 seconds ahead of Alex Marquez in second, with Bagnaia third and Martin a distant fourth.
Reigning world title-holder Bagnaia of Italy now has a 14-point lead on Spain's Martin heading into the race in Qatar next weekend.
The factory Ducati rider Bagnaia is favourite to retain his crown but the championship looks set to go right down to the final race of the season in Valencia at the end of the month.
‘I was struggling’
"I tried to close the gap to Alex and Bastia but I was struggling a lot on the braking," said the 26-year-old Bagnaia.
"But I'm happy we managed to open a bit of a gap in the championship.”
He said that an advantage of 14 points was "not too much" but still "better than nothing”.
"If it was a normal season with one race it's a good gap to manage, but 37 points each weekend (including sprint races) means there's (only) a small gap," he cautioned.
'The Beast' roars
Bagnaia made a sluggish start from pole and was swallowed up right away by rival Martin, who had begun second on the grid on his Ducati, and Bagnaia's team-mate Bastianini.
But Pramac's Martin immediately ran wide and Bastianini, who started third on the grid, emerged at the front.
Bastianini led Malaysian MotoGP sprint winner Marquez through a third of the 20 laps, with Martin and Bagnaia squabbling over third. Bagnaia won that tussle, to cheers from the Sepang crowd.
In fierce heat, the race settled into a groove from there as Bastianini pulled out into a clear lead on Gresini Racing's Marquez, with Bagnaia third and Martin struggling for pace in fourth. It was to remain that way to the end.
Bastianini has suffered an injury-hampered campaign but the Italian nicknamed "The Beast" held his nerve at the front to cruise home for an ultimately comfortable victory, his first of the season.
"I'm tired and I have given 100 percent today," said the 25-year-old.
"After a long time without a podium, today is a victory.
"Only me, my friends, my girlfriend and family knows what I needed to do to arrive here.”
Marco Bezzecchi came into Sepang in third place in the championship and is still mathematically in contention for the world title.
But his unlikely hopes of winning the title evaporated as he rode home in sixth, behind France's former world champion Fabio Quartararo.
The six-time world champion Marc Marquez, older brother of Alex, was 13th on his Honda after starting a lowly 20th on the grid.
Earlier, MotoGP-bound Pedro Acosta was crowned the Moto2 world champion after the Spaniard finished the race in Sepang second behind compatriot Fermin Aldeguer.