It was a night of upsets in the Scottish League Cup, with three Premiership sides beaten by lower-league opposition and two more taken to extra time.
Dunfermline Athletic beat Dundee 3-1 as Fayssal El-Bakhtaoui grabbed a brace for the League One side.
Peter Grant scored the only goal of the game as Falkirk shocked Partick Thistle, and Raith Rovers beat Hamilton Academical 3-1 at Stark's Park.
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.
Elsewhere, Ross Forbes netted as Greenock Morton defeated Queen of the South.
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