Ritchie Humphreys and Ollie Banks fired the League One side into a two-goal lead at the interval and although Wells scored twice either side of Eoin Doyle’s third, Huddersfield were going out until Tommy Lee let a Jon Stead shot squirm under his body in stoppage time.
Wells and Joe Lolley completed the fightback in the first period of extra-time to give Huddersfield the boost they needed after their 4-0 home mauling to Bournemouth at the weekend, which led to the exit of boss Mark Robins.
Chesterfield dominated the first half and went ahead in the 32nd minute when Humphreys planted a header from 12 yards inside Joe Murphy’s right-hand post.
Wells missed a great chance to level in the 39th minute and Chesterfield scored again on the stroke of half-time when Banks, who had only just replaced the injured Humphreys, curled in a 25-yard shot.
The visitors replied in the 58th minute when Wells headed in from 10 yards but Chesterfield restored their two-goal cushion in the 72nd minute as Doyle pounced from close range.
Wells again gave Huddersfield hope by converting an 84th-minute penalty after Stead had been tripped, before Lee’s mistake took the tie into extra-time.
Wells completed his hat-trick in the 94th minute when Lee could only parry a shot from Stead and Lolley punished a defensive error in the 98th minute to settle a pulsating contest.