League Two side East Fife led Motherwell until the 80th minute, when Louis Moult scored to take the game into extra time. Scott McDonald then ensured Motherwell's progress with a 103rd-minute strike and Lionel Ainsworth added gloss to the victory four minutes later.
Forfar Athletic held Premiership leaders Hearts 1-1 after 90 minutes but Callum Paterson came to the rescue in the 105th minute to avoid embarrassment for the Edinburgh club.
Ross County was one of just two Premiership sides to win. Liam Boyce and Michael Gardyne scored as the Staggies eased past Ayr United.
Kilmarnock were held by League Two Berwick Rangers until Rory McKenzie's 70th-minute strike and goals from Craig Slater and Kallum Higginbotham gave the Premiership strugglers a welcome 4-1 win.
The highest scoring game of the night came at St Mirren where a late Declan Gallagher goal saw Livingston through to the third round with a 3-2 victory.
Results in full: Dunfermline Athletic 3-1 Dundee, East Fife 1-1 Motherwell, Forfar Athletic 1-1 Hearts, Kilmarnock 4-1 Berwick, Partick Thistle 0-1 Falkirk, Queen of the South 0-1 Greenock Morton, Raith Rovers 2-1 Hamilton Academical, Ross County 2-0 Ayr United, St Mirren 2-3 Livingston