Mark Corcoran scored a stoppage-time winner as Stenhousemuir recorded only their second away win of the campaign.
The Warriors' only win on their league travels had come back in September, but Corcoran's second goal for the club moved them to within two points of a play-off place.
Ayr United, who could have leapfrogged Martyn Corrigan's side with a win, hit the post through Michael Donald before David Winters headed in from Adam Hunter's cross to give them a first-half lead.
The visitors rallied after the break and Sean Dickson replied with a low header from Darren Smith's centre on the hour mark.
Donald was denied by the woodwork again, this time hitting the crossbar form the edge of the area.
But Corcoran arrowed a low shot into the bottom corner from inside the area to secure all three points.
Ayr United: Graeme Smith, Hunter, McGregor, Christopher Smith, John Robertson, Winters (Wyllie 85), Sinclair, Crawford, Donald, Moffat, Buchanan. Subs Not Used: Brown, McStay, Brownlie, Roberts.
Booked: Crawford. Goal: Winters 33.
Stenhousemuir: Reidford, Ross, Buist, Iain Thomson, McKinlay, Hodge, Rowson, Dickson (Ferguson 84),Smith, Corcoran, Gemmell. Subs Not Used: Shaw, Reid, Exposito-Hepburn, Brash.
Booked: Rowson. Goals: Dickson 60, Corcoran 90.
Ref: Alan Muir