Liam Buchanan notched a fine double as Cowdenbeath raced to bring Ayr's seven-game unbeaten run to an end.
The Fifers opened the scoring in the 23rd minute with their top scorer Buchanan the marksman.
Buchanan doubled the lead after 38 minutes before he turned provider for Matthew Doherty to score from six yards.
Ayr's consolation goal came after 64 minutes through substitute Gareth Wardlaw, who headed home from 10 yards.
Cowdenbeath: Allison, Armstrong, Kenneth, Hill, Baxter, Husband (McBride 52), Patrick Scullion, Docherty (Fusco 79), Ellis, Clarke, Buchanan (Dalziel 90).
Subs Not Used: Fotheringham, Hay.
Goals: Buchanan 23, 38, Docherty 50.
Ayr: McGeown, Brian Reid, Weaver (Waddle 45), Forrest, Pettigrew, Casey, Stevenson, Dunn, Andy McLaren (Wardlaw 52), Shields (Fraser McLaren 78), Vareille.
Subs Not Used: Templeton, Johnson.
Goals: Wardlaw 64.
Ref: J Underhill (Scotland).