Hamilton dented Ayr United's chances of surviving in the First Division with a victory at New Douglas Park.
The Honest Men looked as though they had secured a point but Jon McShane broke their hearts by snatching the winner in the 89th minute.
Mark Roberts' right-footed effort from 18 yards out beat keeper Blair Currie to give the visitors the lead on 17 minutes, but Hamilton took just seven minutes to draw level as McShane drilled a low shot past Kevin Cuthbert.
Ayr handed Hamilton the advantage in the opening stages of the second half when Edward Malone was dismissed for a second bookable offence.
The home side took full advantage in the 58th minute when Daniel Redmond curled right-footed shot into the net from 20 yards out.
Accies' lead only lasted a minute as Michael McGowan's right-footed close range shot made it all square again.
Hamilton secured maximum points when McShane scored from inside the box with one minute of normal time remaining.
Hamilton: Blair Currie, McAlister, McLaughlin, Canning, Hendrie, Redmond, Routledge, Mensing, Gillespie (Kirkpatrick 64), McShane, Ryan (Spence 64). Subs Not Used: Hutton, Kilday, MacGregor.
Booked: Routledge. Goals: McShane 24, Redmond 58, McShane 89.
Ayr United: Cuthbert, Tiffoney, Smith, Malone, Dodd, Moffat, Tomsett, Geggan, McGowan, Parker (Trouten 51), Roberts (Ross Robertson 81). Subs Not Used: McWilliams, McKernon, Armstrong.
Sent Off: Malone (48). Booked: Smith, Malone, Roberts, Tiffoney. Goals: Roberts 17,McGowan 59.
Ref: Craig Charleston