Inter appeared to be cruising after Antonio Candreva and Mauro Icardi scored to give them a 2-0 halftime lead before Alessio Romagnoli pulled one back with seven minutes left. Milan then won a corner in the seventh minute of stoppage-time, Inter failed to clear and Cristian Zapata at the far post managed to force the ball back towards goal.
It hit the underside of the crossbar and was hooked away by Gary Medel but the referee, aided by goal technology, signalled that the ball had crossed the line, prompting wild celebrations among the Milan players.
There was not even time for the game to restart.
The result kept Milan sixth in Serie A, inside the European places, while Inter stayed outside in seventh, two points behind their rival.
The match was played less than 48 hours after Milan were sold to a Chinese-led consortium in a 740m-euro ($788m) deal, following in the footsteps of Inter who have been owned by retail giant Suning Commerce Group since June.
The lunchtime kickoff, the first in 218 meetings of the old rivals, was designed for the Asian television market, though criticised by local fans. Media reports predicted a potential worldwide television audience of 862m viewers.