Rangers maintained their four point advantage over Celtic at the top of the League with their ninth successive victory.
When Rangers went three-up on the hour mark through Chris Burke, having scored two earlier goals through Daniel Cousin and Steven Naismith, it looked as though the home team would stroll to the finishing-line.
However, Gretna striker Kenny Deuchar, who troubled David Weir all afternoon, scored with two late headers to make it an uncomfortable end to the match for Rangers, though substitute Chris Boyd had already scored Rangers' fourth before Deuchar got his second goal.
Carlos Cuellar headed wide from a Burke corner before Rangers took an early lead. Cousin outpaced Danny Hall to Barry Ferguson's through ball before rounding the keeper and shooting into the empty goal.
The home team made it two when Naismith was left unmarked only four yards from goal to side-foot home Ferguson's corner.
Gretna recovered well from these early setbacks and threatened the Rangers goal on a couple of occasions in the first-half. A free-kick by Nicky Deverdics, after Deuchar was fouled, was well saved by Allan McGregor.
However, the Rangers keeper was stranded minutes later when Paul Murray's glancing header from Brendan McGill's cross came back off the post.
Steve Davis broke purposely for the start of the second-half but Burke fired his cut-back over. Rangers added a third after Jean-Claude Darcheville's quick break created the chance for Burke, who made no mistake from six yards.
Gretna's attack had troubled the Rangers defence all afternoon and it was no surprise when they pulled one back from substitute Mickael Buscher's corner, Deuchar scored with a diving header.
Rangers made sure of the points two minutes from the end when Davis's well-timed pass released Boyd, who rounded the keeper before scoring.
There was still time for Gretna to reduce the deficit from another corner. Deuchar's cross was headed back across goal by Hall, allowing Deucher to head home from close-range.