Fifth-placed Roma is six points behind and hosts Empoli on Monday.
"It was important to get back to winning ways, there wasn't much space but we found the goals in the end," Inter midfielder Joao Mario said.
"We did well today, the prospect of overtaking Milan by winning the derby needs to spur us on."
Inter was still without Mauro Icardi, who has not played since being stripped off his captaincy amid protracted contract negotiations. Icardi has said he has a knee injury.
Lautaro Martinez thought he had given Inter the lead after a half-hour but the referee ruled it out for handball following a pitchside review.
Inter appealed for a penalty on the hour mark but the referee opted not to review Kevin Bonifazi's nudge on Antonio Candreva.
The hosts broke the deadlock in the 67th as Lautaro's pass was deflected to Politano who smashed the ball into the back of the net. The goal again went to VAR, on suspicion of offside, but it was given after a lengthy delay.
Inter sealed the match 10 minutes later when Cedric's shot turned into an assist for Gagliardini, who took a touch and fired it into the bottom left corner.
Having seen its title chances diminish, Napoli is now in danger of losing out on second spot.
Napoli drew 1-1 at Sassuolo to leave it 18 points behind Juventus and only six ahead of Milan.
It could have been worse for Napoli but Lorenzo Insigne leveled four minutes from time after a defensive error from Sassuolo captain Francesco Magnanelli, who failed to control a poor cross from Faouzi Ghoulam.
Quagliarella scored from a penalty kick in Sampdoria's 2-1 defeat to Atalanta to take his tally to 20 for the season a personal record for the 36-year-old Italian and one more than Ronaldo for Juventus and Milan forward Krzysztof Piatek.
Quagliarella's penalty came either side of goals for Atalanta from Duvan Zapata, his 17th of the season, and Robin Gosens.
Atalanta moved seventh, level on points with Roma and Torino, with Sampdoria five points further back.
Lazio would have moved level with Atalanta by winning at Fiorentina but the game ended in a 1-1 draw.
Lazio is in eighth place, having played one game less than Atalanta, while Fiorentina stays in 10th.
In other matches, Andrea Belotti scored a second-half brace as Torino came back to win 2-1 at relegation-threatened Frosinone.
Luca Paganini had headed Frosinone in front shortly before halftime to end Torino's run of 598 minutes without conceding.
Frosinone remained five points from safety, but fellow struggler Bologna moved to within one point of 17th-placed Empoli with a 2-0 win over Cagliari.
(This story has not been edited by Business Standard staff and is auto-generated from a syndicated feed.)