While second-placed Gala were edging past Dundee HSFP 17-16, the men from Millbrae eventually saw off the capital side 22-11 at home.
Ayr took a 15-6 lead into the interval thanks to three tries from Nick Campbell, Nick Cox and Robbie Fergusson, while Accies kept themselves in things through two penalties by Ruairidh Bonner.
A try by Sam Pecqueur four minutes after the break put the visitors four points behind before a Hayden Wisnewski try and a conversion by Ross Curle in the 67th minute saw Ayr close in on the title.
Bottom club Boroughmuir will be sorry they didn't add to last week's win over Dundee HSFP against Melrose.
At Meggetland the hosts took a 12-6 lead into half-time thanks to four Ally Warnock penalties. Melrose's points came from Andrew Skeen and Joe Helps' penalties.
After the break the match went to uncontested scrums and then with Boroughmuir's Jackson Achilles in the sin-bin, Melrose went 18-12 up with tries from Allan Dodds and Bruce Dick.
At the end, with visiting skipper Graeme Dodds in the sin-bin, the hosts piled on the pressure, but couldn't add to the score line.
Stirling County were second bottom at the start of the afternoon, but won at Currie to get out of the play-off spot.
A hat-trick of tries from Rory Hughes – all converted – in the last five minutes saw them turn a 21-18 deficit into a 39-21 win.
Aberdeen Grammar scored five tries to beat Heriot's 38-22 at Rubislaw. Cam Eyre, Sean O'Connor, Steven Aitken, Tony McGuinness and Alex Hagart touched down for the hosts while Gavin Cameron and CJ Osazuwa (2) scored Heriot's tries.