Anthony Marenghi scored the winning goal with a free-kick early in the second half.
The win pushed Ayr into second place behind Rangers and alongside Dunfermline Athletic, who came from behind to beat Stranraer 2-1 at Stair Park.
Jamie Longworth put Stranraer ahead after 12 minutes but Stephen Husband equalised after 49 minutes with a 25-yard free-kick and Josh Falkingham scored the winner.
Stenhousemuir lost ground when they were held to a 1-1 draw at home by Forfar Athletic.
John Gemmell gave the hosts an early lead but Forfar equalised through Dale Hilson.
Brechin City held off a late fight-back to beat Arbroath 3-1 at Glebe Park and climb into sixth place, above their opponents on goal difference.
A Graham Hay penalty put Brechin in front before Andy Ryan made it two. Alan Cook pulled a goal back for the visitors with eight minutes left but Bobby Barr made sure of victory.