As the clocked ticked down it looked as if the home side had done enough to seal victory but Sean Lamont had not read the script, and his late try, converted by Calvin Howarth changed the outcome of the match.
A Calum MacRae penalty had seemed enough to clinch victory for Borders until Lamont pounced on a loose pass to streak to the line from 80 metres.
But the winger’s solo effort seconds from time helped Glasgow leapfrog Borders into eleventh spot with 24 points from 16 games.
Glasgow made a brisk start and opened their account with a Calvin Howarth penalty after Dan Kane was caught offside.
Howarth then traded penalty misses with Gareth Morton before Borders launched a series of long-range raids.
Captain Kevin Utterson and Samoan number eight Semo Sititi embarked on strong surges with the latter being hauled down by Rory Kerr just a couple of paces short of the line.
With so much at stake, it was inevitable the match would be tense and jittery but Borders eased their fears when Tanner Vili took over the kicking duties to equalise with a well-struck goal.
Sititi was displaying his skills both in attack and defence and he again almost reached the line before being held in the corner.
Seconds later he backtracked to foil Glasgow scrum-half Sam Pinder, who had made a piercing break from halfway.
There was plenty of action but no more points before the interval. Glasgow boss Hugh Campbell reshuffled his ranks by sending on hooker Scott Lawson for Simon Gunn and Alan Bulloch for Glenn Metcalfe.
He then had to re-jig again when prop Euan Murray was sinbinned for foul play. On went Matt Proudfoot to cover the front-row gap, with Roly Reid being sacrificed for 10 minutes.
After the jittery first half, the contest burst into life immediately after the restart. Bulloch justified his entry to the fray by crashing over for the first try of the match. But Borders replied in positive fashion to snuff out his effort through Morton.
Howarth was handed a clear chance to put the Warriors back in front, but he shook his head in dismay as the penalty flew past the wrong side of the post.
The nerves were also showing at the other end when Morton fluffed an even easier shot at goal.
MacRae took over the boot duties to edge Borders in front – only for Lamont to have the final word, his try being converted by Howarth.