Centres Joe Helps and Ewan Scott fired over two penalties each before Melrose's Australian debutante Todd Pearce claimed the first try of the match. Helps converted and added another penalty to make it 16-6 to the hosts at the interval.
Both sides scored converted tries after the break - Grant Runciman for Melrose and Graham Speirs for Gala. Scott's third penalty then brought Gala to within seven points and they drew level with the same player's conversion of prop Rory Sutherland's last-minute try.
However, Melrose went straight to the other end and won a penalty which Helps calmly slotted between the posts in injury-time.
At the opposite end of the table Currie got their first win of the season, battling to defeat Aberdeen Grammar 23-22 at Rubislaw.
The Edinburgh side had tries from replacement Joe Reynolds and debutante back row Chris Dumont, and teenager George Horne kicked three penalties and two conversions. Erlend Oag, captain Harry Duthie and Rob Cessford scored for Aberdeen, with Mark Ryan kicking a penalty and two conversions.
Heriot's are in second in the table after defeating Edinburgh Accies 30-17 in a capital derby at Goldenacre. Winger Harry Boisseau grabbed two for the victors, and Murray Douglas, Max Nimmo and Colin Goudie also went over. Graham Wilson kicked five points in the bonus-point win.
Accies' points came from tries from Jamie Sole, Nyle Godsmark and Andrew Findlater plus a Stuart Evans conversion.
Ayr also ran in five as they won 32-26 away to Glasgow Hawks at Old Anniesland. Scorers were Calum Templeton, Murray McConnell, replacement Peter Jericevich, Craig Gossman and Robbie Fergusson. Ross Curle and Finn Russell shared the kicking points. Hawks, who picked up a losing bonus point, had tries from Paddy Boyer, James Gormley and Ross Miller.
Hawick whitewashed Stirling County 19-0 at Mansfield Park. Steven Anderson scored a try and Neil Renwick kicked the rest of the points.
Boroughmuir won 43-5 at Selkirk and top the RBS National League.