Former Ayr winger Declan McDaid's 20-yard strike and a Paul McGowan finish on the rebound condemned the hosts to a third straight loss.
Luke McCowan's first-half header cut the deficit, but Ayr United remain behind leaders Dundee United on goal difference ahead of Saturday's games.
Dundee move up a place to fourth.