Troubled Rangers crashed out of the Scottish Cup as Stephen Kenny celebrated his first win at Dunfermline manager.
Dunfermline led 3-0 at one point with goals from Jim Hamilton, Stephen Simmons and Phil McGuire but a Kris Boyd double meant an uncomfortable second half for the home side.
Barry Ferguson was restored to the Rangers team but the captain's armband was retained by Gavin Rae.
But there were early concerns for Rangers when Allan McGregor had to get down low to save a long-range effort from Simmons.
At the other end, Roddy McKenzie saved well to keep out a Karl Svensson header but it was Dunfermline who snatched the lead after 17 minutes.
Owen Morrison's cross was missed by the Rangers defence and Hamilton had time to bring the ball down before lashing it past McGregor with his left foot.
Worse was to follow for Rangers as they lost another sloppy goal in the 29th minute. Simmons' initial shot broke back to him off Svensson and Ian Murray and he swept it into the roof of the net.
McGuire headed in a third from a Scott Morrison corner in the first minute of the second half, but Boyd pulled a goal back when he turned in a Rae cross after 54 minutes.
Boyd then headed a second goal in the 68th minute when he met Murray's cross at the back post.
Ferguson then headed against the crossbar as Rangers dominated but Dunfermline held on for a famous win.