First Division Ayr had high hopes of claiming another Premiership scalp, having knocked out Ross County in the previous round of the William Hill Scottish Cup, and they went ahead in the fourth minute through loan star Aaron Drinan.
But their hopes of another giant-killing act were hindered following a nightmare eight minutes for Aaron Muirhead.
The defender scored an unfortunate own goal to draw St Johnstone level in the 18th minute, he was booked five minutes later and then received a straight red card in the 26th minute for a lunge on Liam Craig.
St Johnstone made their numerical advantage count as within a minute of Muirhead's dismissal, Callum Hendry headed the visitors in front.
The home side took an early lead when Grant Gillespie played a through-ball beyond the Saints defence for Drinan to run onto and the striker, on loan from Ipswich, produced a composed finish as he slotted a right foot shot beyond Zander Clark.
Saints had struggled to get going into the opening exchanges but they drew level in the 18th minute through a Muirhead own-goal.
David Wotherspoon swung in a cross from the left which held up in the wind. Ayr goalkeeper Ross Doohan should have come off his line to claim it but remained rooted to the spot and Hendry and Muirhead contested for the ball and the final touch came off the defender.
That was to spark an eventful eight minutes for Muirhead. He was booked in the 23rd minute for a foul on Hendry and just three minutes later he was dismissed by referee Steven McLean.
Having overrun possession he then lunged in on Liam Craig. Having been off the ground with his challenge, McLean was left with little option but to show him a straight red.
It was to get even worse for the hosts. From the free-kick the ball was played out to Drey Wright on the right flank and he whipped in an inviting cross, which was nodded home by Hendry.
St Johnstone had the ball in the net before the interval but Stevie May was flagged for offside.
Just after the break Doohan produced a fine save to turn Hendry's free-kick, which was heading for the top corner, round the post.
With the wind at their back as Storm Ciara swept in St Johnstone continued to push forward in search of a third and Craig and Wotherspoon went close with long-range efforts which just cleared the crossbar. Wotherspoon was later denied by a fine Doohan save.
May then went close with a chipped effort that clipped the outside of the post before Ayr turned up the pressure late on – bute the visitors held on.