Wayne Rooney had put the Rams ahead in the 63rd minute but Pelly-Ruddock Mpanzu levelled proceedings four minutes later on the sixth anniversary of him signing permanently for the club.
Substitute Donervon Daniels scored his first goal for the Hatters in the 73rd minute but Chris Martin pegged Town back once again with five minutes to play.
The drama continued as the Hatters regained the lead 60 seconds later with Harry Cornick's shot going in off Jayden Bogle.
Max Lowe was sent off by referee Andy Davies in the dying embers and the Hatters stayed strong to record a first victory since December.
Manager Graeme Jones named an unchanged team from the side that lost 3-1 at Nottingham Forest but did welcome back Callum McManaman and Izzy Brown to the bench - the latter for the first time since the defeat at Brentford in November.
The Hatters started quickly and Cornick could have scored the game’s first goal after just six minutes.
Simon Sluga cleared the ball long upfield and Matt Clarke was unaware of Cornick’s presence behind him as the forward latched onto the keeper’s clearance and tried to chip the ball over Ben Hamer, but failed to beat the former Leicester City goalie who stood tall.
The two sides were locked in a stalemate for most of the first half but Cornick did test Hamer again on his left foot, though the goalkeeper was equal to it and saved comfortably.
Derby had plenty of the ball in the second period and went close early on. Hamer produced a clearance up-field towards Jason Knight, who found Tom Lawrence in the middle but his left-footed effort went just over the bar.
Minutes later Lawrence went even closer as he got on the end of Bogle’s cross, hit the ball into the ground and watched it bounce up and off the bar.
The Rams’ pressure paid off in the 63rd minute as Rooney opened his account in Derby colours. He was picked out on the edge of the box and his strike took a deflection off Daniels on its way into the top corner.
Town almost immediately found an equaliser as Cornick was denied at close range by Hamer. The former Bournemouth man was causing problems and again found his way through on goal, this time a perfectly timed block stopped his effort on goal.
But from Bree’s resulting corner, the ball fell to Mpanzu on the edge of the box and he smashed it low into the far corner to send the home crowd into a frenzy.
After replacing Matty Pearson on the hour mark due to injury, Daniels made his mark on the game by scoring a thumping header from Bree’s corner in the 73rd minute to put the Hatters in the lead.
Derby, however, fought back and levelled the game once more through Martin, who powerfully directed Bogle’s cross beyond Sluga with five minutes of normal time left.
Town mounted a charge up-field straight from kick-off and put themselves back ahead a minute later. Harry Cornick’s shot from the right was blocked on the line and bounced off Bogle and into the back of the net.
The madness continued shortly after as Lowe played the ball back to Hamer, who proceeded to pick the ball up and referee Andy Davies awarded a free-kick inside the box, which Derby were furious with.
Rooney and Hamer were booked in the process, but worse still for Derby, Lowe was shown a straight red card for putting his hands on the referee's arm as he held the yellow card aloft.
The Hatters remained calm, with Glen Rea magnificent marshalling the midfield throughout, to earn a valuable three points to move within six points of safety as they travel to high-flying West Brom on Saturday.
TOWN: Sluga, Bree, Pearson (Daniels 60), Bradley (C), Potts, Rea, Mpanzu, Tunnicliffe, Cornick (Berry 90), LuaLua (Brown 68), Collins. Subs not used: Shea, McManaman, Shinnie, Moncur.
Goals: Mpanzu (67), Daniels (73), Bogle o.g (86)
Yellows: Mpanzu
DERBY: Hamer, Bogle, Wisdom, Clarke, Lowe, Holmes (Marriott 79) Bird, Knight (Martin 79), Rooney (C), Lawrence, Waghorn. Subs not used: Roos, Forsyth, Shinnie, Davies, Whittaker.
Goals: Rooney (63), Martin (85)
Yellows: Bogle, Rooney, Hamer, Wisdom
Red: Lowe
Attendance: 10,057 (1,033 away fans)
Referee: Andy Davies