Tuesday night's meeting at St Mirren Park appeared to be heading for extra-time, with nothing to separate the two sides until Mair struck deep into injury-time to ensure the Buddies' name would be in the draw for the next round.
St Mirren boss Danny Lennon made three changes from the side who slumped to defeat to Kilmarnock at the weekend for the clash with Hamilton.
Graham Carey, Dougie Imrie and Sam Parkin were drafted into the team and Steven Thompson, David van Zanten and Gary Teale were dropped to the bench.
Accies were without strikers Jon McShane and Kieran Miller through injury, with injury doubt Andy Ryan on the bench, while on-loan St Johnstone forward Stevie May was unavailable for selection.
The first chance fell to Saints in the opening minute when Kenny McLean burst into the box only to drag his shot just wide of the upright.
At the other end, Hamilton should have opened the scoring when Louis Longridge raced on to a James Keatings ball and saw his effort fizz across the face of goal and wide when he should have hit the target.
Keatings then had a go himself but he rifled well over the bar, with Craig Samson untroubled between the sticks.
Saints were threatening again when what appeared to be a harmless cross come shot from Imrie took a major deflection and a full-stretch Kevin Cuthbert was called into action to tip just over.
The home side made their first change 10 minutes before the break when Lewis Guy was replaced by Thompson, with it unclear whether the substitution was down to injury or not.
With the tie still deadlocked at half-time following a fairly drab first half, Saints made another switch for the second half, with McLean making way for van Zanten.
Jim Goodwin could have sparked this match into life shortly after the restart but sent his angled drive blazing over the bar instead as both sides struggled to find the opener.
Accies threw Ziggy Gordon into the action for Gary Fisher on the hour mark with nothing to separate the teams.
Saints looked slightly more threatening in front of goal and a thunderous 25-yard free-kick from Carey forced an impressive save from Cuthbert.
The Accies goalkeeper came to the rescue again moments later, this time to tip a close range header from Carey past the post as the home side pushed for a late winner.
Saints' pressure finally paid off deep into injury time when Mair threw himself in front of Carey's corner and his header found the back of the net with seconds to go.