Two late goals from Ryan McGuffie killed off plucky Ayr after a hard-fought match at Somerset Park.
John O'Neil struck on 20 minutes to put Gretna ahead when he collected a cross from Stevie Tosh and fired home from 16 yards.
But Ayr refused to buckle and were level just seven minutes later when Paul Weaver put Jerome Vareille through to score.
And just as Ayr were getting on top full back James McKinstry stuck out a leg to divert a Tosh cross into his own net and put Gretna ahead at the break.
United looked to have gained a deserved point when Craig Conway scored from the spot after a David Nicholls handball with just five minutes to go.
But Gretna went straight up the park and McGuffie struck from close range to make it 3-2.
The league leaders then rubbed salt in the wounds when McGuffie went down in the box and converted himself from the spot to make it 4-2.
Ayr will be pleased by their performance in running the visitors close but their winless run now stands at 13 games and leaves them in deep trouble at the bottom.