Hernandez broke the deadlock from the penalty spot in first-half stoppage time after a late challenge from Fabiano Parisi.
History was made in the second half when Milan brought on Francesco Camarda to become the youngest debutant in Serie A history, aged 15 years, 8 months, 16 days.
"Francesco is a good lad; he is working well and we also need his freshness. He is very young but also very mature. We were all very happy for him. I could be his grandfather," Milan's 58-year-old manager Stefano Pioli told Sky Sports Italia.
Francesco Camarda made Serie A history when coming on for his debut in AC Milan's win over Fiorentina.
Giuseppe Cottini/AC Milan via Getty Images
Milan are third in the table with 26 points, five behind leaders Inter Milan. Fiorentina are sixth with 20 points.
Pioli said his side had made progress after their lacklustre run of matches.
"We had a good first half, then mentally it couldn't be our best performance and yet we made progress there by suffering, holding out and bringing home the points," he said.
"I am sure that when we get results a bit more consistently, we can then get back to playing our beautiful style of football. There are moments where you have to do things this way."
Fiorentina nearly equalised around the hour mark when Nico Gonzalez's cross deflected off a Milan defender and clipped the far post.
Christian Pulisic started for Milan in his first appearance since Nov. 7 after recovering from a hamstring injury sustained against Paris Saint-Germain. The United States international nearly scored early on but his effort was palmed over the bar by Terracciano.
Reuters and The Associated Press contributed to this report.