As Ayr threatened in the early stages, Ryan Scully saved well from Craig Malcolm and Alan Forrest had a shot deflected just wide.
But Dunfermline opened the scoring after eight minutes with a stunning long-range strike from Ross Forbes and took a grip on the game after that.
Lawrence Shankland, guilty of missing a simple chance earlier in the half, then scrambled home the hosts' second on the stroke of half-time after a Forbes shot had struck both posts.
Ayr tried to get back into the game in the second half but were punished when Dunfermline scored on the counter-attack, Shankland striking with a great finish into the far corner from the edge of the box.
Martin Grehan hit a first-half hat-trick as Stranraer strengthened their grip on third place with a 3-1 win away to Brechin City. Grehan took his total to 18 goals for the season with his treble, the second of which came from the penalty spot.
Andy Jackson pulled a goal back in the second half but Brechin's hopes of saving something ended when Paul McLean was sent off.
Forfar Athletic almost let a three-goal lead slip at Arbroath but held on to win 3-2. Dale Hilson and a double from Gavin Swankie had the visitors coasting but Arbroath hit back through Alan Cook and Leighton McIntosh in the final 20 minutes.
East Fife and Airdrieonians shared a 0-0 draw at New Bayview. The home side thought they had won it in the closing stages when Liam Buchanan connected inside the six-yard box but Grant Adam pulled off a wonder save.