Brad McKay was the match winner, scoring only four minutes after coming off the bench for the injured Coll Donaldson, but his manager John Robertson insisted he got lucky with the switch rather than claiming it was inspired.
“It’s a massive win,” Robertson said. “We started the game really well with our Achilles heel penalty and miss but we stuck at it.
“Ayr were much better in the second half and got on top and our goal came against the run of play.
“It takes us closer to our play-off target but we haven’t even thought about the semi-final and won’t do until Tuesday.
“Ayr have been terrific all season and deserve to be in the play-offs and I will be delighted if we play them again.”
Inverness were presented with a dream chance to make a flying start when they won a penalty after only seven minutes.
Nathan Austin raced on to a Liam Polworth through pass and was brought down as he attempted to take the ball wide of Ross Doohan.
The keeper was booked but made amends when he brilliantly saved the kick taken by Aaron Doran.
Calvin Miller had a shot diverted off the line by Austin as Ayr began to threaten before Doran went close twice in two minutes for the visitors.
Miller raced clear as the Honest Men started the second period on the front foot but his teasing cross found no takers.
Andy Geggan was then wasteful with a great chance to give Ayr the lead when he blazed over from a Miller cutback.
Inverness soaked up the pressure and then struck on the break.
Donaldson hobbled off after 62 minutes to be replaced by McKay and the newcomer wasted no time in making an impression.
Joe Chalmers fired over a free-kick which eluded Michael Rose and the ball ran to McKay who forced it home at the back post.
Ayr, who at one stage led the league by five points, have now slipped to fourth but still have games in hand on their rivals.
Manager Ian McCall said: “I thought after the penalty we were the better team in the first half. Miller and Shankland caused them all sorts of problems, it was just our final ball.
“We were well on top then in the first 25 minutes of the second half we literally battered them with three or four great opportunities to score, didn’t do it and then made a poor error at their goal and couldn’t get back in it.”