Swindon Town's promotion challenge took a knock while Wrexham restored home pride with a hard earned win in the rain and blustery wind.
Chris Llewellyn cut inside and beat Rhys Evans from a narrow angle to put the home side a goal up midway through the first half and nearly scored again a couple of minutes later, but his shot hit Matt Heywood and bounced away.
Wrexham were caught straight after half-time when they failed to clear a corner, allowing Sammy Igoe to collect the loose ball and smash it in off a post.
Defender Brian Carey headed narrowly over the Swindon crossbar from a Ferguson free-kick as Wrexham pushed up and the home side soon regained the lead.
Paul Edwards made ground on the left, Llewellyn crossed into the box and Carlos Edwards steer his angled shot past Evans at the back post.
Steve Thomas seemed to kill the game off when Chris Armstrong turned the ball back to the edge of the area for the midfielder to hit it first time, giving Evans no chance, but Swindon fought their way back into the game to make a close finish.
Andy Gurney reduced the lead with a header after Michael Ingham missed Igoe's corner, but the keeper made amends by holding Tommy Mooney's last-gasp header to deny Swindon a point.
A delighted Wrexham manager Denis Smith said: "That was as good a game as you will ever see and we scored three great goals.
"Conditions were very difficult but the lads adjusted to it and I was pleased with the way they did their job." Swindon manager Andy King said he was surprised they hadn't lost by a greater margin: "If we play as badly as that again we might never win another game," he said.
"Wrexham are not a physical team, but they out-fought us and I am annoyed."