Icardi equalised almost immediately after Bonaventura opened the scoring shortly before half-time but the Argentinian shot wide of an open goal with two minutes remaining.
Bonaventura duly headed home the winner and it finished 2-1 as Atalanta registered a fourth straight victory that moved them into ninth place in the Serie A table.
Inter remain one point behind Fiorentina, whose game in hand takes them to Napoli later on Sunday.
After struggling for results over the festive period Inter had stabilised and were looking to take fourth place with a third straight win.
But the team from Bergamo were the brighter side in the early stages and regularly got themselves in good positions to test goalkeeper Samir Handanovic.
Guglielmo Stendardo forced a firm punch from the Slovenian before Luca Cigarini was denied from a first-time shot as Handanovic showed quick reflexes to make the save.
Atalanta came even closer in the 19th minute, German Denis clattering the crossbar with a header from Bonaventura's cross.
Inter, who had already worried Andrea Consigli with a drive from Rodrigo Palacio, looked like opening the scoring when Icardi beat the offside trap to shoot straight down the Atalanta goalkeeper's throat.
Palacio had been well-placed to tap home had his countryman slipped the ball across the area.
In the 35th minute, Atalanta unlocked the game when Bonaventura smashed past the flailing Handanovic from outside of the box.
The hosts were riled and responded in the best possible way.
From the restart, Icardi danced straight into the area, skipped over the challenge Mario Yepes and slipped the ball into the bottom corner.
With winning runs to be extended and targets to be reached over the season run-in, neither club was prepared to settle for the draw and the second half was suitably frenetic.
Fredy Guarin thundered a 25-metre screamer against the bar before Palacio clattered the base of a post with a driven header.
Cigarini was soon denied by Handanovic as Atalanta pushed back upfield.
Andrea Ranocchia should have done better with a header from Guarin's free-kick as Inter went all out for the win but there was drama at the other end as Jonathan cleared Franco Brienza's effort off the line.
The moment that may give Inter nightmares for some time to come arrived in the 88th minute.
When Jonathan slipped the ball into Atalanta's six-yard box, it seemed nothing could stop Icardi from slotting home a dramatic winner.
The Argentinian inexplicably blasted wide, however, and he was punished almost immediately.
On the stroke of full-time, Handanovic dropped to his knees after watching Bonaventura nod home his second goal, this time from a Brienza free-kick.
It was Inter's first home defeat since October 5 and suddenly a return to Europe seemed much further away.