SUPER-SUB Levi Mackin netted an extra-time winner as the reborn Blues won their first FA Cup tie at the second attempt.
Dom Collins, an early replacement for the injured Paul Linwood, gave Neil Young’s men a deserved 65th-minute lead when he was credited with his first goal for the club.
That was the way it stayed until the 89th minute when Gainsborough substitute Jamie Yates scored a dramatic late equaliser to take the clash into extra-time.
Opportunities were few and far between in the additional 30 minutes as both teams tired.
But midfielder Mackin took the one that came his way right at the start of the second period to ensure it will be Chester who host Blue Square Bet North rivals FC Halifax Town in the third qualifying round on Saturday.
Blues boss Young made one change to the side that started the stunning league success at Stalybridge Celtic as captain George Horan returned in place of Collins.
Collins hardly put a foot wrong on Saturday but the fit-again Horan was given the nod because of his physical presence.
It was certainly needed against a Trinity team who proved as obdurate as they had done in the initial 1-1 draw between the sides.
Against such well-drilled opposition, the easy option for Chester would have been to go more direct in a bid to create clear openings.
Instead they took the patient approach and, with a little more luck and sharper finishing, they would have been ahead at half-time.
Nathan Jarman flashed a header wide from a Marc Williams cross before Scott Brown went closer still with a 22-yard drive.
Antoni Sarcevic then went on a thrilling run which ended with him being brought down just outside the area. From the free kick that followed Wes Baynes curled a superb effort inches past the post.
Sarcevic, so impressive against Stalybridge, was at the heart of everything the Blues did well and he came desperately close to breaking the deadlock just before the break.
The young midfielder showed strength and skill to turn his marker in the box before dinking a shot over Jan Budtz that just evaded the incoming Iain Howard before trickling wide of the upright.
The hosts were nearly punished for not making their territorial dominance count.
Firstly Michael Leary blazed over after a corner was not cleared before John Danby got down low to his left to make a smart save from a Dominic Knowles free kick.
Five minutes later and Chester were in front. A long-range Baynes free kick took an awkward bounce and was turned around the post by Budtz.
From Matty McGinn’s resulting corner Collins was adjudged to have tapped the ball over the line although the final touch may have come off a Gainsborough defender.
The visitors were forced to come out of their shell and Terry Hawkridge was not far away when he tried his luck from 20 yards after he cut inside on to his left foot.
Their hopes of an equaliser would have been ended had Baynes not been unlucky to see his viciously struck 20-yard free kick cannon back off the bar.
And relived by the let off, Trinity levelled the contest one minute from time through Yates.
The ball eventually found its way to Yates after Danby had punched clear a corner and the sub showed superb skill to send a left-footed half-volley into the roof of the net from 18 yards.
Incredibly the Lincolnshire outfit went close to winning it in injury-time when Knowles saw his close-range header brilliantly kept out by Danby.
But extra-time was needed and it was Mackin, 20 minutes afterr coming on, who emerged the hero when he side-footed home from 16 yards after Williams played the ball into his path.
Chester: Danby, Baynes, Horan, Linwood (Collins 36), McGinn, Hankin (Booth 106), Brown, Sarcevic (Mackin 86), Howard, Jarman, M Williams. Subs: Fearon, Powell, Clancy, A Williams.
Booked: Jarman, Baynes.
Goal: Collins 65, Mackin 106.
Gainsborough: Budtz, Roma, Waterfall, Hone, Young, Wilde (Nelthorpe 60), Leary (Banks 114), D’Laryea, Clarke (Yates 74), Hawkridge, Knowles. Subs: Williams, Ridley, Karkach.
Goal: Yates 89.
Referee: Simeon Lucas (Darwen).