St Johnstone progressed to the fourth round of the Scottish Cup with a last-gasp winner at Somerset Park.
Jason Scotland (right) opened the scoring for St Johnstone
Jason Scotland put Saints in front after five minutes with a fine 20-yard free-kick into the bottom corner.
Ryan Stevenson fired a superb equaliser on the half-volley after 66 minutes, when he flicked the ball over Kevin James' head and rifled home.
In extra time, Ayr keeper Mark McGeown was in outstanding form but was beaten by Peter MacDonald's late strike.
Both sides found it difficult to play attractive football in treacherous conditions and high winds.
Stevenson, a former Saints player, scored the equaliser in Ayr's first threatening attack of the second half.
He flicked the ball over James' head before hitting a sweet half-volley into the net.
St Johnstone looked the more likely to score in extra time, but were denied on a number of occasions by the outstanding McGeown.
But he was powerless to stop MacDonald's shot-on-the-turn with four minutes of extra time left - just as penalties looked likely.
St Johnstone now face Falkirk at the Falkirk Stadium in the fourth round.
Ayr: McGeown, Forrest, Campbell (Weaver 46), Robertson (Pettigrew 36), Lowing, Caddis, Casey, Dunn, Stevenson, Vareille (Strain 95), Wardlaw. Subs Not Used: Friels, Johnson.
Booked: Lowing, Caddis, Wardlaw.
Goals: Stevenson 66.
St Johnstone: Cuthbert, James, Mensing, McManus, Lawrie, Hardie (Jackson 95), McInnes, Sheerin (McLaren 73), Stanic, MacDonald, Scotland. Subs Not Used: Anderson, Halliwell, Dyer.
Booked: Lawrie, James, Stanic, McInnes, McManus.
Goals: Scotland 5, MacDonald 116.
Ref: M Tumilty