An injury-time winner from Jon Shaw booked the Hatters’ place in the last 16 of the FA Trophy.
The striker’s effort from 10 yards crossed the line a minute into three added minutes to prevent Northern Premier League Matlock earning a replay at Kenilworth Road.
The Town led 1-0 at the break thanks to Simon Ainge’s first-half header – after Shaw had headed a Jake Howells corner into the defender’s path.
The Gladiators rallied in the second half and levelled from the penalty spot when McMahon fired past Dean Brill after Ainge was harshly adjudged to have fouled Phil Roe.
Having been pegged back the Town went all out in search of a winner. But after Stuart Fleetwood and Janos Kovacs were frustrated by efforts hitting the woodwork the pressure paid off late on when Shaw struck to earn a place in the third round on 12th January.
It all began with Paul Buckle making three changes to the side that lost in midweek at Newport. Brill replaced Mark Tyler in goal whilst Kovacs and Shaw came in for Lathaniel Rowe-Turner and Dean Beckwith.
Northern Premier League side Matlock began with no recognised striker on the pitch but had the first half-chance after just a minute when Gareth Davies’ dangerous low cross was smothered by Brill as the Gladiators looked for some early initiative.
Back came the Town, and on four minutes Gray spurned a great chance to put his side ahead when he poked a shot at goal from six yards after Ronnie Henry’s right-wing cross was dummied by Scott Rendell.
Matlock were making the Hatters work hard but the visitors edged ahead on 17 minutes. Gray won a corner that Howells hung in from the right to the back-stick. Shaw was first to it, heading back across goal, and there was Ainge to send a bullet header past Jon Kennedy in the Matlock goal.
With their tails up, two minutes later JJ O’Donnell fired in a low shot that Kennedy needed to get down well to keep out as the Town pushed for a quick-fire double.
It was almost 2-0 on 23 minutes as the Town continued to dominate. Henry, the supplier of two goals in his last two games, floated in a perfect right-wing cross that was met by the head of Shaw but the striker’s effort bounced half-a-yard wide.
Six minutes later the Hatters were denied a second when somehow a combination of goalkeeper and defence kept the score at 1-0. Firstly Gray’s fierce first-time drive from a Howells cross was spilled by Kennedy but, with the goal at Rendell’s mercy Phil Roe, the Gladiators’ left-back, was on hand to clear the danger – hacking a clearance out that hit Rendell on the way out of play that almost spun into the net.
Matlock were offering little in attack and Alex Lawless almost fashioned a chance out of nothing on his own and he eased past a number of home players to the edge of the box but saw a tame left-foot shot roll harmlessly into the arms of Kennedy.
With a busy Christmas period approaching a slight knock to Gray saw him substitute nine minutes before the break. Limping slightly, he was taken off and on came Fleetwood.
Matlock made a change at the break bringing on-loan Chesterfield striker Jacob Hazel but it was the Town that had the first chance of the second half when Ainge saw a header stopped by Kennedy following O’Donnell’s high left-wing cross.
The Town were playing some nice football and a flowing move on 51 minutes, involving Lawless, Rendell and Fleetwood, cumulated in Howells firing a well-hit left-foot effort narrowly over the crossbar.
However, for all the Hatters’ possession they failed to double their advantage and, just after the hour, the Gladiators were level when McMahon fired home from the penalty spot after Ainge was adjudged to have leant into Roe.
Level, the Town almost restored their lead straight from the restart when Fleetwood let rip with a 35-yard strike that rattled the crossbar with Kennedy beaten all ends up.
All of a sudden it was all the Hatters as they aimed to get their noses back in front. O’Donnell saw a shot blocked and Kovacs had a header comfortably saved by Kennedy.
Keeper Kennedy then came to the Gladiators’ rescue with 14 minutes left when he did well to save a rasping low effort from Howells after the Town man had run onto Shaw’s lay-off.
As time ticked down, Henry burst into the box and saw a handball shout waved away by the referee but, unbelievably, the Town hit the woodwork again with three minutes left when Kovacs’ header hit the post and the ball dropped in to the grateful arms of Kennedy when meeting Howells’ right-wing corner.
Three added minutes were added by the officials and just when it seemed the sides would meet again in a replay at Kenilworth Road on Tuesday up popped Shaw with a late, late winner. O’Donnell won a free-kick in the far corner and, after the ball was half-cleared, Shaw smashed an effort goalwards that was deemed to go over the line by the assistant referee.
TOWN: Brill, Henry, Ainge, Kovacs, Howells, Mendy, O'Donnell (sub Rowe-Turner 90), Lawless, Shaw, Rendell, Gray (sub Fleetwood 36). Subs not used: Lacey, Dance, Robinson.
MATLOCK: Kennedy, Harcourt, Roe, Foster, Yates, Lukic, Grayson (sub Hazel 46), Ashmore, Cotton, McMahon, Davies. Subs not used: Leesley, Gaughan, Ellington, Buxton.