Inter moved third, two points above Milan and six ahead of fifth-placed Roma. The top four qualify for next season's Champions League. The Nerazzurri were eliminated after the group stage of this year's competition and also went out of the Europa League on Thursday.
"Our history is clear, we are Inter so it's normal for us to be criticized after getting knocked out," coach Luciano Spalletti said. "We did well to put it straight behind us, showing the right attitude again as a team.
"You need to have players who can turn things around completely, they are the ones that always make the difference. Everyone had Milan as the favorites and it wasn't easy to take charge of the game." The Rossoneri came into the match on a run of five successive league wins. In contrast, Inter had won only one of its last five matches in all competitions, had a number of players out with injury and was still without forward Mauro Icardi. He has not played in more than a month, since being stripped off his captaincy amid protracted contract negotiations.
"Only crazy people and idiots could have thought that today would be a walk in the park for us," said Milan coach Gennaro Gattuso. "We gave too much of the pitch to our opponents and that wasn't a problem only of the defense but of the whole team. We didn't have the right attitude."
Inter took the lead with less than three minutes on the clock. Lautaro headed down a cross from Ivan Perisic and Vecino was left unmarked to volley the ball into the roof of the net. Inter extended its lead six minutes after the break when, following a short corner, De Vrij headed in Matteo Politano's cross.
Milan pulled one back six minutes later when Bakayoko headed in a free kick for his first goal for the Rossoneri but Inter restored its two-goal advantage shortly after as Lautaro drilled home a spot-kick after Politano had been felled by substitute Samu Castillejo.
There was a frantic finale after Musacchio reduced the deficit again in the 71st, tapping in after Inter goalkeeper Samir Handanovic managed to prevent an own-goal from Danilo D'Ambrosio. Milan had two chances cleared from practically off the line in stoppage time.
FIRST DEFEAT Without Cristiano Ronaldo, Juventus fell to its first Serie A defeat of the season as it lost 2-0 at Genoa on Sunday. Substitute Stefano Sturaro, who was sold by Juventus in January after a lengthy injury spell, scored two minutes after coming off the bench for his Genoa debut and Goran Pandev sealed the result.
Ronaldo wasn't even included in the Juventus squad because the 34-year-old was given a rest following Tuesday's hat trick against Atletico Madrid that saw the Bianconeri overturn a first-leg deficit to reach the Champions League quarterfinals.
It was Juve's first league defeat since April but Massimiliano Allegri's side still has a commanding advantage at the top of the table. Second-placed Napoli trimmed the gap to 15 points as it fumbled a two-goal advantage over Udinese but recovered to win 4-2.
Napoli goalkeeper David Ospina was hospitalized after he played on despite a head injury and then collapsed. OTHER MATCHES Atalanta's European hopes were damaged by a 1-1 draw at home to bottom club Chievo Verona.
Atalanta slipped to seventh, six points below Milan and level on points with Lazio, which beat Parma 4-1. Coach Aurelio Andreazzoli steered Empoli to a 2-1 win over fellow struggler Frosinone in his first match back in charge.
Empoli moved back out of the relegation zone, a point above 18th-placed Bologna.
More AP soccer: https://apnews.com/apf-Soccer and https://twitter.com/AP_Sports