Mark McKenzie scored an 88th-minute winner as Ayr clinched second place in the Scottish Championship with a 2-1 victory over Inverness.
Josh Mullin gave the visitors the lead in the 34th minute after being played clean through and they held on to their advantage until eight minutes from time, when Nathan Shaw equalised.
A draw would have meant both teams missed the play-offs but a late winner would have secured a top-four finish for whoever scored.
And it was the visitors who got it as McKenzie proved to be the hero in the 88th minute by scoring from Daire O’Connor’s cross.
There was still time for a nervous moment when Austin Samuels had the ball in the net for Scottish Cup finalists Caley Thistle but it was ruled out for offside.
Subscribe or register today to discover more from DonegalLive.ie
Buy the e-paper of the Donegal Democrat, Donegal People's Press, Donegal Post and Inish Times here for instant access to Donegal's premier news titles.
Keep up with the latest news from Donegal with our daily newsletter featuring the most important stories of the day delivered to your inbox every evening at 5pm.