Milan looked destined to take all three points from Bergamo thanks to Franck Kessie's second-half goal against his former team in a match that saw two red cards, but Andrea Masiello headed in at the death in an incident which is likely to bring further scorn on Donnarumma after a poor showing in the Coppa Italia final against Juventus.
The away side showed nothing to suggest a win was their primary aim in the first period, offering very little going forward – Atalanta just about looking more threatening.
But Milan made the most of Atalanta's profligacy as Kessie produced an emphatic finish from the edge of the box at his old stomping ground with half an hour to play, just before Rafael Toloi was dismissed for sarcastically applauding the referee.
Riccardo Montolivo then followed him down the tunnel for a poor tackle and, while Milan looked set to hold on, Masiello nodded in in the second minute of stoppage time, with Donnarumma making a mess of what should have been a routine save.
The result means both sides will be in the Europa League next season, but as it stands sixth-placed Milan will go straight into the group stage.
Given the importance of the contest, it was hardly surprising to see such a cagey start, with neither side taking control.
Milan's only notable efforts in the first half hour were from distance, but neither Suso nor Giacomo Bonaventura managed to cause Etrit Berisha a great deal of bother with their respective shots.
The first clear-cut opportunity of the opening period fell to Atalanta in the 25th minute, as Marten de Roon found Musa Barrow in the area and he sidestepped Alessio Romagnoli before scuffing harmlessly wide of the left-hand post.
Milan had an even luckier escape just before the break - Lucas Biglia crucially blocked two close-range efforts from Cristante and Remo Freuler in quick succession, before Leonardo Bonucci managed to get in the way of Robin Gosens' powerful follow-up.
Atalanta continued to look the sharper of the two sides after the break and went close within two minutes of the restart – Cristante's right-wing cross forcing Donnarumma to scramble across his line and make a save.
Cristante was at the centre of things again soon after, drawing a save from Donnarumma with a header, before Mattia Caldara inexplicably put the rebound wide.
Atalanta received a further blow soon after when referee Marco Guida sent Toloi off for a show of dissent after Milan were awarded a free-kick.
The numbers were soon evened up when Montolivo went in hard on Alejandro Gomez 15 minutes from time, and that was just the encouragement Atalanta needed.
Masiello met Josip Ilicic's sumptuous cross with a header deep into stoppage time and, although Donnarumma looked to have saved it, he palmed the ball into his own net to confirm European football for Atalanta next season.