Italy ended Scotland's Euro 2008 dream and ensured their own qualification for next summer's finals thanks to a Christian Panucci stoppage-time goal.
Christian Panucci (left) scored the late winner for Italy
Luca Toni had stunned the Scots by stabbing the ball home from five yards with less than two minutes gone.
Andrea Pirlo cleared a David Weir header off the Italian line.
But Ferguson forced the ball home when Lee McCulloch's effort was saved after 65 minutes only for Panucci to head home from a disputed Pirlo free kick.
The Scots believed that Alan Hutton had been fouled and the resulting goal meant that Italy qualify along with France, with the Scots in third, before both have to play their final Group B games in midweek.
Italy coach Roberto Donadoni had chosen Panucci ahead of Massimo Oddo at right-back and Gianluca Zambrotta instead of Fabio Grosso on the left.
Andrea Barzagli stood in for the injured Marco Materazzi at the heart of the defence.
But Italy were more settled in attack and Toni, who had scored both goals when they beat Scotland at home in March, showed how lethal they can be within two minutes.
Three Scotland players were drawn towards the throw-in taker and Antonio Di Natale had plenty of time to gather in the box and drive it low to the front post, where Toni finished from five yards.
Scotland manager Alex McLeish had chosen a 4-1-4-1 formation designed to avoid such an early setback.
But, despite having only James McFadden up front, the Scots replied by taking the game to their more illustrious opponents.
Claims for a penalty were correctly waved away by referee Manuel Enrique Mejuto Gonzalez when a McCulloch drive struck Zambrotta.
Ferguson blazed a volley over from the edge of the box and sent another drive straight at Gianluigi Buffon, while an Alan Hutton header flew a foot wide.
Captain Barry Ferguson (left) had equalised for Scotland
Scotland were fortunate not to concede a second goal when Di Natale was incorrectly flagged for offside while turning the ball home after goalkeeper Craig Gordon had done superbly to block a powerful drive from Ambrosini.
It was Italy who rode their luck on the stroke of half time when a Weir header was cleared off the goalline by Pirlo.
The Hampden crowd did its best to roar the side forward after the break.
But the Italians appeared to have subdued the fire of the Scots on the pitch until one of Hutton's trademark surges towards the opposition penalty box won a free-kick.
McFadden's drive took a deflection off the wall, McCulloch's drive was blocked by Buffon but Ferguson stabbed it home on the rebound.
Scotland went in search of a winner, with McLeish replacing Scott Brown with Kenny Miller.
McFadden fired into the side netting with Miller lurking in the six yard box.
And McFadden could only turn the ball wide on the slide as he attempted to turn in a Miller cross at the back post.
Panucci missed from two yards from a Pirlo free-kick as Italy threatened on the break.
But the defender made amends from five yards after free kick that looked like it ought to have gone Scotland's way and instead led to the home side's heartbreaking elimination from the competition.
Scotland: Gordon, Hutton, Weir, McManus, Naysmith, Brown (Miller 74), Fletcher, Hartley, Ferguson, McCulloch (Boyd 90), McFadden.
Subs Not Used: McGregor, Alexander, Caldwell, Pearson, Robson.
Booked: Naysmith, McCulloch.
Goal: Ferguson 65.
Italy: Buffon, Panucci, Cannavaro, Barzagli, Zambrotta, Camoranesi (Chiellini 83), Gattuso (De Rossi 87), Pirlo, Ambrosini, Di Natale (Iaquinta 68), Toni.
Subs Not Used: Amelia, Oddo, Perrotta, Gilardino.
Goals: Toni 2, Panucci 90.
Ref: Manuel Enrique Mejuto Gonzalez (Spain)