Substitute John Maisano hit his first goal since signing in the summer as Ayr hit back to force their seventh goal from 10 league games.
Alloa looked set for an unlikely win when they grabbed a short lead after 13 minutes. Jose Quitongo headed against the bar and Andy Brown pounced to force the ball home from close range.
Ayr went all out for the equaliser, and Craig Conway had a fierce shot cleared off the line by Richard Walker. Then Gareth Wardlaw hit the top of the bar with a spectacular overhead kick seconds before the interval.
For all Ayr's pressure Alloa should have grabbed a second goal after 61 minutes, but Quitongo shot tamely against the legs of keeper Mark McGeown when he should have scored.
The turning point came when Ayr introduced Maisano for Paul Hyslop. He quickly made his mark lashing the ball home after Wardlaw set him up at the edge of the box.
As Ayr went all out for the winner, Maisano had a shot saved and then nodded just wide from an Andy Reid cross.