Mario Pasalic scored with one minute remaining as nine-man AC Milan ended their crisis with an astonishing 1-0 Serie A victory at Bologna.
Vincenzo Montella's men came into the match having lost four straight games in all competitions - three in Serie A - and avoided a defeat that would have sealed their worst run of league losses since April 1986.
After Gianluigi Donnarumma had made a superb save from Ladislav Krejci, Milan sealed the most unlikely of victories when Gerard Deulofeu set up Pasalic to score from close range despite the hosts' numerical advantage.
Bologna made five changes after they were humiliated by a 7-1 loss at home to Napoli last time out and this result is arguably just as unimpressive.
Milan's first half did not go to plan as they lost both starting centre-backs. Alessio Romagnoli came off through injury before Gabriel Paletta received his third red card of the season, getting a second yellow for a foul on Blerim Dzemaili.
Juraj Kucka was next to see red after receiving two yellows in the space of six second-half minutes, but Montella's re-shuffled pack survived before winning in the final moments.
Seventh-placed Milan are now just two points behind rivals Inter in fifth, while Bologna stay 13th.
Milan should have taken an early lead when Deulofeu capitalised on a mistake from Emil Krafth but saw his effort saved by Angelo Da Costa at the near post, with Pasalic firing over a golden chance on the rebound.
Bologna threatened for the first time when a purposeful run into the box from Krejci saw him surge past Paletta's challenge before being denied by a brave block from Milan goalkeeper Donnarumma.
The hosts then went closer when Donnarumma initially fumbled Simone Verdi's cross and had to react promptly to stop Dzemaili tapping in.
Milan, who made three changes, saw a new name added to their long injury list when Romagnoli was forced off, Cristian Zapata replacing him.
And the night went from bad to worse for Montella's men after 36 minutes when Paletta - already booked for shoving Ibrahima Mbaye - picked up another for bringing down Dzemaili as he sprinted towards the penalty area.
Montella brought on defender Gustavo Gomez for midfielder Manuel Locatelli at the break and the visitors made a steady start to the second half, Deulofeu forcing another save from Da Costa.
But Milan were down to nine men by the hour-mark, Kucka clipping Adam Nagy to earn a second yellow only six minutes after fouling Dzemaili for his initial booking.
Carlos Bacca had clashed with Montella having been taken off against Sampdoria at the weekend, but was more understanding of the circumstances this time around as he was withdrawn for Andrea Poli in the head coach's final change.
It took a magnificent save from Donnarumma to keep Milan on level terms, Krejci's first-time effort kept out from eight yards after he latched on to Dzemaili's cutback.
But it was not all one-way traffic, Deulofeu leading a promising Milan counter-attack and finding Pasalic, who saw his left-footed strike saved by Da Costa.
Deulofeu then saw his corner bounce against the crossbar as Milan threatened victory, but Mattia Destro volleyed Verdi's cross just off-target at the other end.
Verdi curled a free-kick over, but Donnarumma did not have a major late save to make before Pasalic struck after Deulofeu had superbly beaten Krafth with a nutmeg and crossed into his path as Milan secured a morale-boosting triumph.