Billy Reid's First Division outfit put up a decent fight and had their opponents anxiously defending the lead at times but their SPL visitors held on for the win to earn a home meeting with Ross County in the next round.
Saints named an unchanged side from the one who lost 5-2 to Hearts at the weekend, while Accies were boosted by the return of Mark McLaughlin after he missed Friday's win over Partick Thistle through suspension.
Hamilton had a great chance to open the scoring early on when Dougie Imrie nicked in to steal the ball from the grasp of goalkeeper Craig Samson and set up the shot for Jim McAlister who lashed wide of the upright.
The visitors gradually began to impose themselves upon the match and David van Zanten was not too far away with a powerful shot from 20 yards.
The original tie had ended in goalless stalemate but the replay served up a goal after 21 minutes.
It was Saints who surged into the lead when Carey unleashed from the edge of the box and sent a firm low shot past goalkeeper Tomas Cerny and into the bottom corner.
Accies could have hit back when Ali Crawford created space for himself for the shot only to squander a decent chance when he blasted well over and failed to trouble the Saints goalkeeper.
McAlister then stung the palms of Samson with a blistering point-blank effort but the home side failed to find an equaliser before the break.
Accies emerged for the second half determined to haul themselves back into the tie and Dundee United target Imrie was unfortunate not to find the back of the net with a swerving effort that fell wide.
At the other end, Carey could have grabbed his second of the night but he, too, dragged his shot past the post.
Carey was threatening again moments later, this time with a curling free-kick that looked destined for the top corner before Cerny produced a great save to claw the effort away from goal.
With 20 minutes to go, Hamilton knew there was still time to rescue the tie and Imrie turned provider with a cross from the left for Lee Kilday but he saw his close-range header smothered on the line by Samson.
The home side continued to push for the leveller and Samson was called into action again to claim a bullet header from Accies top goalscorer McLaughlin.
Only the woodwork prevented Hamilton from drawing level with just three minutes to go when an audacious 35-yard strike from Imrie crashed off the post before the Saints defence frantically cleared the danger and held on for the win.