Dunfermline had trailed 3-0 early in the second half after John Gemmell, with his fifth goal of the season, Ross Smith and Sean Lynch all found the net.
Quickfire goals from Jordan Moore and Andy Geggan brought Dunfermline back into it, but their resurgence looked to be killed off when Sean Higgins made it 4-2 after 73 minutes.
But not a bit of it, as Dunfermline scored three times in the final seven minutes through Alan Smith, a Moore penalty and Geggan's second.
Brechin City edged Forfar Athletic 2-1 in another dramatic game. Craig Molloy and Alan Trouten gave them a 2-0 lead, but Iain Campbell pulled back a late penalty before seeing a second penalty saved by Graeme Smith.
East Fife beat Arbroath 2-1 with both sides ending up with 10 men. Scott Durie, who was one of those ordered off, put East Fife ahead and Liam Buchanan made it 2-0 with a penalty after Colin Hamilton was sent off for conceding it. Alan Cook scored for Arbroath.
Stranraer and Ayr United shared a 1-1 draw. Jamie Longworth gave Stranraer a seventh-minute lead when he fired home from Sean Winter's cross, but Michael McGowan equalised eight minutes from the break with a left-foot shot from the edge of the area.
Ryan Borris almost won it for Stranraer at the death, but his shot hit the crossbar.