Martin holds off wounded Italians to seal San Marino GP double
The Spaniard finished 1.350 seconds ahead of Marco Bezzecchi of VR46 Racing, who was hampered by a wrist injury he suffered at the Catalan Grand Prix.
Ducati's MotoGP world champion Bagnaia, who had his leg run over in a separate crash last week, finished third.
Bagnaia is top of the riders' standings with 283 points, with Martin (247) in second and Bezzecchi (218) in third.
After his sprint and race double this weekend and a third-placed finish in Catalunya, Martin has reduced Bagnaia's lead by 30 points in just two weeks.
"Today, with Marco and Bagnaia, it was going to be really tough. I tried to stay in front for all the laps and as soon as I saw a bit of a gap I started pushing like hell," Martin said.
"Winning here in Italy, in front of the Italian guys, that's big."
As they did in Saturday's sprint race, the trio of Martin, Bezzecchi and Bagnaia shot off the line and built an early gap to the rest of the pack, turning the race into a three-way shootout.
While the sure-footed Martin led throughout the race, Bezzecchi had to contend with early disappointment, after he briefly overtook Bagnaia for second but then immediately ran wide on the next turn to fall back into third.
However, as the race wore on, the injuries from the Catalan GP appeared to take their toll on Bagnaia, and Bezzecchi once again leapfrogged him in the 19th lap. On the second time of asking, the 24-year-old made the move stick.
"I gave my all from the beginning to the end. After I passed Pecco, I was able to get the rhythm even though the pain was coming more and more. I wanted this podium with all my heart," Bezzecchi said.
A fading Bagnaia required a Herculean performance to finish on the podium, fending off a string of late attacks in a hard-fought battle for third with wildcard Dani Pedrosa.
"I was pushed by the fact that I wanted to do a good race for the fans," said the injured Bagnaia, who had to be helped off his bike after the race.
"I was destroyed after half the race. Happy with the result, it wasn't easy considering the situation."
Veteran Pedrosa's creditable fourth-placed finish gave KTM some reason to cheer, after they suffered a double blow when Brad Binder and Jack Miller crashed in successive laps.
Maverick Vinales (Aprilia) and Miguel Oliveira (RNF Aprilia) were fifth and sixth, while six-time MotoGP champion Marc Marquez ended in seventh.