United sprang one of the shocks of the day with a fine victory that leaves Premiership outfit Hamilton playing catch-up in Group G of the Betfred Cup.
Ayr made the perfect start when referee Willie Collum spotted an infringement in the box and Alan Forrest beat debutant Remi Matthews from the spot.
The setback prompted an immediate reaction from Accies who forced a series of corners but without reward.
Ian McCall’s Hamilton continued to press and Alejandro D’Acol forced an important save from Greg Fleming.
At the other end Matthews was on his toes to prevent Forrest from doubling the lead with a powerful shot.
Hamilton almost drew level five minutes before the break when Ali Crawford fed Dougie Imrie with a short free kick but his fierce 22-yard shot was well saved by Fleming.
Instead United doubled their advantage within a few minutes of the restart when Robbie Crawford’s left-foot shot from outside the box found the net.
Hamilton desperately needed a quick reply and they almost scored when a Louis Longridge shot was deflected just wide.
Accies suffered a blow when Garci Tena went off injured to be replaced by Scott McMann.
United continued to give as good as they got and the Championship side looked just as capable of increasing their lead as their Premiership opponents did of reducing it.
Longridge notched a consolation goal for Hamilton in injury time but Ayr held out for a deserved win.