Olivier Giroud scored a late winner and was sent off for his celebration as Milan beat Spezia 2-1 to go second in Serie A.
Theo Hernandez put the Rossoneri on course for back-to-back wins after they hammered Salzburg to qualify for the Champions League round of 16 in midweek.
Daniel Maldini, son of Milan legend Paolo, then stunned his parent club by scoring Spezia's first away goal of the season to equalise in the second half.
There was late drama at San Siro, though, as Giroud came off the bench to volley an 89th-minute winner before being shown a second yellow card for whipping off his shirt.
Milan started strongly and Rade Krunic smashed against the crossbar after cutting in from the right before they took the lead in the 21st minute.
Hernandez ghosted into the box unmarked and controlled a pinpoint pass from Ismael Bennacer on his chest before calmly finishing from close range, the goal finally given following a long VAR check for offside.
M'Bala Nzola wasted two glorious chances to equalise, rounding Ciprian Tatarusanu before blazing wide then somehow failing to convert from point-blank range after Rafael Leao's deflected strike rattled the bar at the other end.
Bartlomiej Dragowski dived to his right to keep out Divock Origi's strike early in the second half, but Spezia continued to pose a threat and Albin Ekdal's header flashed over the bar.
The Rossoneri failed to heed the warning and Maldini stunned his employers just before the hour mark, finding the bottom corner of the net with a measured right-footed finish.
Milan thought they were back in front courtesy of a stunning effort from substitute Sandro Tonali, but the goal was ruled out for a foul by Fikayo Tomori on Nzola following a VAR check.
The champions were not to be denied, however, as Tonali provided a brilliant cross for Giroud to volley in at the back post before being given his marching orders in a dramatic finale.
What does it mean? Milan make hard work of it
Victory for Milan moved them two points ahead of Atalanta after they were beaten 2-1 by leaders Napoli earlier on Saturday.
Pioli's men are six points adrift of the unbeaten Partenopei, who have won all but two of their 13 matches.
There was a huge sense of relief following Giroud's late strike against a team who are languishing in 17th place, with Milan having suffered a defeat at Torino before putting Salzburg to the sword in a 4-0 rout.
Giroud makes another big impact
Striker Giroud scored two goals and provided as many assists in that thumping of Salzburg and made another big impact off the bench.
After replacing Origi in the 72nd minute, he scored with his only shot and then made an early exit in a busy cameo.
Mixed feelings for Maldini
Spezia had failed to score in six Serie A matches on their travels before visiting the champions, losing all six of those games.
Maldini got them off the mark and opened his Serie A account for the club before Giroud spared him what might have been a tricky conversation with his father, who saw his son find the back of the net 5,333 days after he last did so at San Siro.
Key Opta Facts
– Since the start of last season, only Borussia Monchengladbach's Ramy Bensebaini has scored more goals (eight) than Hernandez (seven) among defenders in Europe's top five leagues.
– Since the start of 2022, Milan have won 12 home matches in Serie A. No team has had more home successes in the competition in the period (also 12 for Inter).
– Before Giroud, the last Milan player to score and be sent off in Serie A was Alessio Romagnoli against Sassuolo on November 28, 2021.
– Spezia faced 15 shots in the first half of this match; only once have they faced more in the first half of a Serie A match: on October 31, 2021 against Fiorentina (18).
Milan face a trip to Cremonese on Tuesday, when Spezia are at home to Udinese.