Former Celtic and Sheffield Wednesday forward Simon Donnelly marked his St Johnstone debut with an extra-time winner to fire his team into the second round of the Bell's Cup.
Saints had an uphill task on their hands after Ross Forsyth was sent-off for a professional foul after just four minutes, having brought down Steve Convery.
Second Division Hamilton took advantage and scored the opener in the 39th minute, when Chris Aitken hammered in a 25-yard shot.
But First Division Saints drew level just before the hour, when Mixu Paatelainen headed home Keigan Parker's cross.
Parker put Saints in front six minutes later, when he ran on to a through ball from Donnelly to score.
But Hamilton hit back after 78 minutes through Brian McPhee.
Donnelly grabbed the winner in extra-time and also rattled the post with another effort.
Hamilton: McEwan, Fitter, Steven Thomson, Lumsden, Forbes,
Carrigan, Sherry, Aitken, Corcoran, McPhee, Convery.
Whiteford, Maxwell, Ferguson, Waddell, Jellema.
St Johnstone: Cuthbert, Robertson, Dods, Stevenson, Forsyth,
Reilly, Maxwell, Fotheringham, McLaughlin, Parker, Paatelainen.
Subs: McCluskey, Donnelly, Baxter, Malone, Nelson.
Referee: S Dougal