A hat-trick from Steven Fletcher gave Hibernian their first home league win since September and edged them into the top six in the SPL.
Colin Nish, on his home debut, gave Hibs an early lead and then Fletcher scored twice to seemingly put the home team on easy street.
But adventurous Gretna had other ideas and two goals in the final nine minutes from Gavin Skelton and Kenny Deuchar had Hibs on the ropes.
It was only when Fletcher completed his hat-trick from the penalty spot after goalkeeper Greg Fleming had fouled Nish, earning a red card, that Hibs were assured of the points.
It was Gretna who seized the initiative at the start, with their on-loan teenager John Paul Kissock forcing a brilliant early save from Yves Makaba-Makalamby.
But Hibs moved in front after ten minutes when Fletcher wriggled free on the left and provided a low cross for Nish to prod the ball home from six yards.
Gretna continued to come at Hibs, but Fletcher put the Edinburgh side two in front after 19 minutes with a wonderful strike from fully 30 yards, after Gretna defender Craig Barr had headed the ball into his path.
It looked to be enough to see off Gretna but the bottom club started the second half well and it took another fine save from Makalamby to touch over a shot from Kissock to preserve Hibs' two-goal advantage.
Fletcher made it 3-0 after 58 minutes. Deuchar gave the ball away to Filipe Morais and his deep cross was chested down by Fletcher before he thundered the ball high into the net.
The Hibs striker had earlier seen a shot come back off the underside of the crossbar and Gretna had no answer to his strike power.
The Borderers came back strongly in the final ten minutes after substitute Brendan McGill gave them more width on the right.
He supplied the cross for Skelton to pull a goal back with a right foot shot in 81 minutes.
Worse was to follow for Hibs when Deuchar scored Gretna's second with a close-range header with two minutes left.
All ended well enough for Hibs with Fletcher completing his hat-tick from the penalty spot, with Deuchar replacing the red-carded Fleming between the sticks.