Two late goals grabbed Peterhead three valuable points in their quest to keep in contention for a promotion play-off spot.
The Blue Toon dominated the majority of the match but despite plenty possession of the ball, visiting keeper David Hutton was rarely called into action.
The win was even more worthy given that Manager Jim McInally was without key players.
Rory McAllister was still suspended, Dean Cowie absent with a knee injury and Jamie Stevenson deemed not fit enough even for a place on the bench.
The first half was poor in terms of chance creation from both sides.
The Honest Men looked a threat early on but after Ryan Donnelly then Jordan Preston showed glimpses that the visitors might be set to repeat their impressive 4-1 win the previous week.
That proved to be a false dawn though as the home side took control.
However, it was the 19th minute before they actually posed any sort of a threat, an Andy Rodgers flick by the post going wide.
Their best chance so far came just after the half hour, a David Cox corner, a Scott Ross header and a bicycle kick from Rodgers tipped over by Hutton.
The keeper was forced to reproduce that save eight minutes later, this time from a flying Cox header.
The dominance continue after the break but actual shots on goal were few and far between and with only 11 minutes remaining, a goalless draw look very much on the cards then out of the blue, like buses, two came along in quick succession.
The opener was from a sweet delivery by Jordan Brown.
He had only been on the pitch for a matter of minutes when he sent over a free kick from just beyond the left side of the penalty area to the back post and Gary McDonald, making his home debut, rose to nod past an Ayr rearguard that looked unbreachable.
A second goal would take the pressure off and it duly arrived four minutes from time.
It was a needless goal to give away, United full back Nicky Devlin had no need to challenge Fraser McLaren who, like Brown, had on just come on, but he clumsily barged into him on the left side of the area and Rodgers did a job from the spot that McAllister would have been proud of.
Next up is on-form Morton on Saturday. They beat leaders Stranraer to set themselves up for a game that will be vital for both sides in what is set to be a cracker at Balmoor.
Peterhead – Smith, Kerr, Noble, Strachan, Ross, McDonald, Riley (McLaren 80), Redman, Rodgers, Dzierzawski, Cox (Brown 75)
Unused – Jarvie, Donaldson, Smith
Referee – Alan Newlands
Attendance – 560