Farid El Alagui and Robbie Crawford were the goal heroes but Dumbarton shot themselves in the foot with a red card for Garry Fleming.
Dumbarton took the lead when a quick break by Lewis Vaughan set up Robert Thomson who swept home a low shot.
Ayr equalised with the last touch of the first-half. El Alagui saw a header touched on to the bar by Alan Martin but the striker reacted quickest to convert the loose ball.
And six minutes after the break Crawford drove into the box and finished well to turn the game.
Alan Forrest spurned a great chance for Ayr when he sprinted clear from the halfway line only for keeper Martin to dash off his line and smother his shot.
Dumbarton’s chances took a dent when sub Fleming was sent off for a challenge on Ross Docherty. However, it took great saves from Greg Fleming to deny Thomson and Gregor Buchanan as the ten-man visitors finished strongly.