The leaders looked to be coasting when they led Stranraer 3-1 deep into the second half but Ryan Thomson struck twice in three minutes to bring his side level.
However, Ayr had a sting in the tail, Paddy Boyle settling it with five minutes left.
Ryan Wallace had put Stranraer in front but Declan MacDaid equalised late in the first half. Craig Moore and Michael Moffat then put Ayr in a commanding position.
Raith Rovers also came from behind to beat Forfar 3-1 and stay two points off the top.
Forfar went in front after 18 minutes when Mark Millar netted his fifth of the season.
Lewis Vaughan equalised for Raith before Jamie Bain was sent off for Forfar to leave them in trouble.
Liam Buchanan put Raith in front with five minutes left and Greig Spence added the third.
East Fife won 3-2 at Arbroath, taking the lead after just three minutes through a Thomas O’Brien own goal.
Steven Doris equalised after 15 minutes but Nathan Flanagan put the Fifers back in front. Chris Duggan added a third before the break after goalkeeper David Hutton was caught in possession. Danny Denholm pulled a goal back for Arbroath but East Fife held on.
Ryan Tierney scored an early double as Airdrieonians beat Albion Rovers 2-1 at Cliftonhill. Tierney flicked home a cutback from Jake Hastie after three minutes and, after Joao Vitoria equalised, he scored what proved to be the winner in the 16th minute.
Adam Martin scored the only goal with six minutes left as Alloa edged Queen’s Park 1-0.