The match was one of only two to go ahead in the top flight, with those at Dundee HSFP, Melrose and Stirling County postponed due to the weather.
Heriot's went close early on with some good runs through back-rows Fraser Brown and Jason Hill, but it was Ayr who opened the scoring after 18 minutes. Their try was set up by winger Kerr Gossman, who advanced down the flank before nudging a clever kick through. Centre Ross Curle was on the same wavelength and managed to gather the ball and touch down, but stand-off Finn Russell could not convert.
The home side were level just six minutes later, their unconverted try coming through hooker Kevin Bryce, and they took a deserved lead with 11 minutes of the first half remaining when captain Graham Wilson kicked a penalty. Ayr lost replacement centre Robbie Fergusson to a 32nd-minute yellow card, but levelled through Russell's penalty five minutes later.
With Fergusson still off the pitch as the second half began, however, the home side took advantage and a good team move ended with winger Michael Paterson scoring an unconverted try.
Ayr were up against it at that point, but scored two converted tries in a seven-minute spell once back up to 15 men. They both came from Scotland and Glasgow Warriors prop Jon Welsh – on his way back from injury – with Russell adding the extras. That put the visitors 22-13 ahead, but Heriot's kept going and in the 68th minute Ayr second-row Nick Campbell was yellow carded and Wilson's penalty put the hosts just six points behind.
In the closing stages, Heriot's pushed hard for the converted try they needed, but it never came and Ayr held out for an important victory.
In the other match, Gala kept up the pressure on the leaders by seeing off a stuffy Currie 17-6 at Netherdale. Currie took an early lead through a penalty by full-back Jamie Forbes before Gala went 7-3 up when winger Grant Somerville scored against his former club and stand-off Lee Millar converted.
A penalty try followed for the home side before another Forbes penalty made the interval scoreline 14-6 in their favour.
The second half was a battle of attrition and neither side could break down the other. As a result, the score stayed the same until injury time when Millar made sure of victory with a penalty to leave Gala six points behind Ayr.