Allan Jenkins scored four minutes from time to give Gretna their first-ever Scottish Premier League win and prevent Dundee United going joint top.
Allan Jenkins came off the bench to secure victory
Prince Buaben had knocked home the opener for United from close range, but David Cowan volleyed in five minutes later then headed his second.
Lee Wilkie rose to head home the equaliser after the break.
But substitute Jenkins squeezed in the winner after a quick break and a lay-off from Evan Horwood.
Gretna boss David Irons had introduced Nicky Deverdics, Erik Paartalu and David Cowan in place of Abdul Osman, Craig Barr and Brendan McGill.
Stuart Duff replaced the injured Christian Kalvenes for United, while Morgaro Gomis returned from suspension to displace David Robertson in midfield.
And the visitors started confidently, Buaben giving them a ninth-minute lead when he stroked home from close range after Noel Hunt had knocked Willo Flood's centre across goal.
Gretna left-back Horwood claimed he had been fouled, but referee Charlie Richmond ruled Hunt's shoulder charge was fair.
And Gretna were level five minutes later when Cowan volleyed home from 12 yards.
Goalkeeper Grzegorz Szamotulski had pushed Chris Innes's 40-yard strike past the post but he had no chance from Cowan's strike after Fabian Yantorno's corner had sparked a goalmouth scramble.
The goal lifted Gretna and Yantorno tested Szamotulski with a curling 30-yard free-kick that the United keeper pushed over the bar, while Gavin Skelton volleyed over from a tight angle.
Lee Wilkie (right) had looked like giving United a point
Barry Robson saw a similar free-kick saved at the other end before Hunt shot wide from 16 yards after being set up by Flood.
But it was Gretna who went ahead in the 36th minute, when full-back Cowan headed his second following a corner amid some hesitant United defending.
Gretna goalkeeper Greg Fleming did well to parry clear a couple of Robson free-kicks, while United defender Darren Dods headed just wide.
Aurelien Collin almost put the game beyond them when he headed Yantorno's free-kick against the bar and McMenamin's volley bounced into Szamotulski's hands.
But United were level within seconds as Lee Wilkie nodded home Robson's 65th-minute corner from three yards.
However, Jenkins headed the winner out of the blue when he met Horwood's cross to guide the ball into the corner.
There was still time for Fleming to block Robson's powerful effort and scrambled the ball to safety for a corner.
Cowan proved a hero in his own penalty box when he cleared Garry Kenneth's header off the line and Gretna played out three minutes of injury time to the delight of their fans.
Gretna: Fleming, Cowan, Innes, Collin, Horwood, Paartalu, Skelton, Murray, Deverdics (Jenkins 73), McMenamin, Yantorno (Buscher 90).
Subs Not Used: Scott, Barr, Graham, Osman, Hogg.
Booked: Collin, Yantorno.
Goals: Cowan 14, 36, Jenkins 86.
Dundee Utd: Szamotulski, Dillon, Wilkie, Dods, Duff, Robson, Gomis (Robb 67), Buaben, Flood (Kenneth 87), Jordan Robertson, Hunt.
Subs Not Used: McLean, Kerr, David Robertson, Cameron, Goodwillie.
Goals: Buaben 9, Wilkie 65.
Ref: C Richmond