The 2,000 supporters who were back inside Fir Park were stunned by Ruari Paton's quickfire double just before the break. The striker netted from close range before his deflected 20-yard strike beat Liam Kelly.
And Motherwell turned the game on its head inside three minutes. Lamie headed home following a short corner in the 72nd minute before substitute Kaiyne Woolery netted the winner from 15 yards.
The victory means Motherwell will win Group F on Wednesday if they beat Airdrie, who were held to a goalless draw by Queen's Park before claiming a bonus point.
Livingston fell to a 2-1 defeat at Alloa after Barry Ferguson's men hit two goals in the final eight minutes.
Conor Sammon missed a series of chances as the hosts enjoyed the better of the first half before Bruce Anderson hit his first goal for Livingston in the 63rd minute. The former Aberdeen striker fired into the roof of the net following Jackson Longridge's low cross.
Scott Taggart cut inside to slot home the equaliser and Alan Trouten grabbed the winner from the spot four minutes later after Ayo Obileye had been penalised for a push.
Cowdenbeath moved ahead of Livi on six points in Group D after Kris Renton's second goal earned them a 3-2 win over Brechin in the 89th minute at Central Park.
Charlie Mulgrew's clever free-kick handed Dundee United a 1-0 win over Arbroath which took them three points clear of their opponents in Group B.
Mulgrew and Logan Chalmers faked a mix-up before the former Celtic player fooled goalkeeper Derek Gaston by curling the ball inside the near post midway through the first half.
United dominated the first half but the visitors improved and Dale Hilson came close on a couple of occasions. United held on with 10 men for the final 15 minutes after Archie Meekison went off injured with all substitutions used.
Nathan Austin hit a double as Kelty Hearts beat East Fife 3-0 in the other game in Group B.
Kilmarnock regained the three points they lost after fielding a suspended player against East Kilbride, as they beat Clyde 2-1 at Broadwood.
They wasted no time in making up lost ground when Blair Alston netted in the first minute and Innes Cameron's penalty doubled the lead before Ally Love pulled one back from the spot.
Killie are three points behind Group G leaders Stranraer, who beat East Kilbride 1-0 thanks to a Matty Yates strike.
Hamilton came from behind to beat 10-man Falkirk 2-1. Aidan Nesbitt put the hosts ahead just after the break but the game turned in the 78th minute when Gary Miller was sent off for a pull in the box. Ronan Hughes netted from the spot before Jamie Hamilton scrambled home the winner.
Accies moved top of Group E as Albion Rovers and Edinburgh City played out a 1-1 draw with the Lanarkshire side claiming the bonus point.
Forfar moved on to eight points at the top of Group C after Mark Gallagher's late goal was enough to earn victory at Brora.
Dunfermline thrashed Dumbarton 5-1 to join St Mirren on six points in Group H. Nikolay Todorov, Dom Thomas, Craig Wighton and Kevin O'Hara were on target for the Pars while Gregor Buchanan put through his own net. Ross MacLean hit a consolation.
Partick Thistle got their first win as second-half goals from Zak Rudden and Brian Graham saw them come from behind to beat Stenhousemuir 2-1.
Inverness came from two goals down against Stirling in Group A but had to settle for a point which leaves them two off leaders Hearts.
Dylan Mackin and Kieran Moore struck inside 20 minutes for the visitors before Roddy McGregor and Shane Sutherland countered. Albion won 3-2 on penalties.
Peterhead beat Cove Rangers 3-1 in a north-east derby in the other Group A encounter.