Notts County initially led through a masterful John Bostock free kick, yet that only acted as the catalyst for one of the most thrilling second halves of the season.
Paul Mullin and Jacob Mendy put Wrexham ahead before abysmal defending from both sides took over, Kyle Cameron equalising with a free header before Notts County gifted Wrexham a third goal through Elliot Lee.
However, the drama was far from over. Notts County were awarded a penalty in added-on time, only for Foster to pull off a stunning stop to his right to deny Cedwyn Scott, albeit the goalkeeper seemingly being off his line.
Wrexham now hold a three-point lead over Notts County at the top of the standings, also possessing a match in hand on their rivals with a match in hand.
Given the stature of the two clubs and with the teams locked on the same points, this was billed as one of the biggest-ever National League fixtures, yet the game did not necessarily live up to its billing in the first half.
Chances were few and far between with nerves naturally a factor, even with the referee who failed to award Wrexham a penalty when Cameron pushed over Paul Mullin with little intention of playing the ball.
Up the other end, Cameron forced Wrexham goalkeeper Foster to divert the ball behind from distance, but the visitors would go in front just seconds before the half-time whistle with a quite stunning free kick from Bostock.
Despite being in a central position, the former Tottenham Hotspur youngster had work to do to beat Foster from 25 yards, yet he made the task look easy with a left-footed effort into the veteran stopper's left-hand corner.
While Wrexham were left stunned, the break came at the right time, and the home side were level within four minutes of the restart through Mullin, who won the physical battle with the defender before converting first-time from 12 yards having been found inside the penalty area.
Clear-cut opportunities remained at a premium, although Wrexham nearly went in front on the hour mark when Eoghan O'Connell's thunderous volley from 10 yards struck the crossbar and bounced clear.
After the game had entered the final quarter, Wrexham completed the turnaround. Mullin expertly beat the offside trap to get down the flank to tee up Jacob Mendy, who was calmness personified at the back post as he found the top corner from an acute angle.
However, just as it appeared that Wrexham were in ideal shape to close out one of the biggest wins in their recent history, they immediately became nervy in defence.
Notts County had passed up two openings in quick succession before making no mistake with the third, Cameron powering a header into the far corner after being left all on his own at the back post following a corner.
If Wrexham's defending for Notts County's equaliser was bad, Geraldo Bajrami's defending for Wrexham's third was even worse. The Albanian defender tried to flick a cross clear, rather than ruthlessly see off the danger, and the ball only fell to Mullin who teed up Lee to fire home from eight yards.
With three minutes left, Sam Dalby was sensationally denied by Sam Slocombe from close range, and it appeared that he would be left to rue that missed chance when Notts County were gifted the opportunity to end a pulsating encounter with a share of the spoils.
O'Connell seemingly put out his hand to deny a goalscoring chance and could have no complaints with the decision, yet Foster was equal to Scott's well-struck effort to his right and got away with advancing marginally off his line.
Wrexham also survived a corner in the aftermath before they celebrated what could prove to be one of the biggest results in their history, Phil Parkinson's team left requiring seven points from their last four matches to achieve a return to the EFL.