Despite only having 38 per cent possession, Cagliari created seven chances to Atalanta’s five.
Atalanta took the lead, however, in the 13th minute when Gianluca Scamacca met Ademola Lookman’s low ball into the box with a first-time chip over the keeper.
Tommaso Augello equalised for Cagliari three minutes before the break when he finished Eldor Shomurodov’s square ball convincingly.
Just as it was looking as though the game was destined for a draw, Nicolas Viola sent the stadium into rapture in the 88th minute with a glancing header into the far post.
Cagliari have moved up to 13th on the table with a four-point buffer from the relegation zone.
Atalanta are sixth, five points behind Roma in fifth but with a game in hand.
Want access to every Serie A and Serie B game live and on demand? Click here.