Hernandez scored from a corner in the 92nd minute to leave Milan, still unbeaten this season, top of Serie A with 34 points, one ahead of Inter Milan who earlier won 2-1 at Verona for their seventh successive league win.
Even so, coach Stefano Pioli played down talk of winning a first Serie A title since 2011 and tiptoed around the subject.
"I continue to keep enthusiasm low for that, Juventus and Inter are the favourites for the Scudetto," he said. "We have to consider the investments made by the respective teams, the history of the players and the championships.
"If the table is still like this in April, we would be thinking differently. Now let's think about winning every match, then we'll see where we can get to."
Rebic put Milan in front in the 10th minute with a looping header from a Calhanoglu cross and the hosts went further ahead seven minutes later. Patric knocked over Rebic in the area, the referee awarded a penalty and Calhanoglu converted.
Lazio were also awarded a penalty following a VAR review after Joaquin Correa was tripped by Pierre Kalulu in the 28th minute. Immobile stepped up and saw his kick saved by Gianluigi Donnarumma but Luis Alberto snapped up the rebound.
Lazio equalised with a trademark goal just before the hour as Sergej Milinkovic-Savic floated a ball over the Milan defence and Immobile met it with an angled volley for his ninth league goal of the season.
Lazio looked capable of winning the game, but instead it was Milan who stole the points as Hernandez continued his excellent season by grabbing the winner with almost the last touch of the game.