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.