Dunfermline manager Stephen Kenny won his first match in nine attempts to knock Rangers out of the Scottish Cup.
Dunfermline celebrate Jim Hamilton's opening goal
Jim Hamilton blazed in the opening goal and Stephen Simmons shot home the Fifers' second after 28 minutes.
Phil McGuire headed the Pars further ahead after the break before Kris Boyd reduced the gap with a shot and header.
Barry Ferguson and Boyd went close for the visitors, who parted company with manager Paul Le Guen on Thursday, but the home side held on for victory.
Ferguson was recalled to the Rangers midfield after being stripped of the captaincy and dropped by Le Guen, but Gavin Rae continued to lead the side under the guidance of temporary manager Ian Durrant.
Dunfermline had not scored in their last five matches but immediately looked dangerous against a rudderless Rangers.
Simmons came close to opening the scoring with a speculative 35-yard drive but keeper Allan McGregor managed to turn the skidding ball past the post.
Rangers' Karl Svensson had a header saved by Roddy McKenzie and Alan Hutton was guilty of a shocking miss when his close-range header cannoned off the back of Pars defender Owen Morrison.
Dunfermline had failed to open the scoring in any of their matches this season but broke that dismal record after 17 minutes.
A deep cross from Scott Morrison was missed by the Rangers defence and found Hamilton, who took a touch and slammed in a powerful angled drive from six yards.
Jim O'Brien, making his Dunfermline debut after moving on loan from Celtic, powered a header on target but team-mate Stevie Crawford got in the way.
Rangers battled back into the game in the second half
However, the home side managed to double their lead soon after thanks to some calamitous Rangers defending.
Hutton could only head a cross straight up in the air and the ball fell to Scott Morrison, who sent an overhead kick into the path of Simmons.
The Pars man scuffed a shot against Svensson only for the ball to ricochet off Julien Rodriguez and back to the home midfielder.
Simmons then gleefully blasted a shot high into the net from eight yards out.
Five minutes before the interval, Rae squandered a chance to reduce the deficit when he headed a Charlie Adam corner wide.
Rangers needed a positive start to the second half but the visitors found themselves further behind inside two minutes of the re-start.
Morrison swung in a corner and McGuire out-jumped Hutton to crash the ball home.
Another mix-up in the Rangers defence saw the ball fall to Hamilton but the striker's awkward scooped shot was saved by McGregor.
Rangers hit back with a neatly worked goal on 54 minutes when Rae ran on to a cute Nacho Novo back-heel and cut the ball back for Boyd to slam home.
Boyd then got on the end of an Ian Murray cross to find the net with a close-range header.
With Dunfermline pinned back, Ferguson hit the bar with a header and Boyd fizzed a shot inches wide.
But, despite the incessant pressure, Dunfermline held on to leave Rangers' season in tatters.
Dunfermline: McKenzie, Wilson, McGuire, Scott Morrison, Young, Ross, Simmons, Owen Morrison (Woods 67), Crawford, O'Brien (Bamba 76), Hamilton (Daquin 83). Subs Not Used: Murdoch, Tod.
Booked: Simmons, Young, Wilson. Goals: Hamilton 17, Simmons 29, McGuire 46.
Rangers: McGregor, Hutton, Rodriguez (Burke 34), Svensson, Murray, Rae, Ferguson, Hemdani, Adam (Buffel 64), Kris Boyd, Novo. Subs Not Used: Gallacher, Lennon, Papac.
Booked: Novo, Kris Boyd, Adam, Hutton. Goals: Kris Boyd 54, 68.
Ref: I Brines