Leicestershire 380 and 109 for 5 (Cosgrove 49) lead Derbyshire 362 (Madsen 76, Raine 5-66) by 127 runs
Derbyshire put themselves in a position to secure their first championship win of the season after turning the tables on Leicestershire on the third day of the second division match at the Fischer County Ground.
The visitors' lower order and tail batted with huge determination in the first half of the day, ensuring Leicestershire's first innings advantage was just 18, before the bowlers picked up five wickets to leave the Foxes leading by only 127 with five second innings wickets in hand at the close.
Resuming on 199 for 6, Alex Hughes and Alex Mellor, the latter making his first-class debut, batted without giving a chance in raising a 50 partnership for the seventh wicket. Having done so, however, and with a personal half-century within reach, Hughes had his off stump knocked out by Richard Jones, the delivery keeping a touch low as it burst through the batsman's defences.
Mellor continued to acquit himself impressively, showing an admirably calm temperament in the fact of testingly straight bowling before being bowled off the inside edge by Ben Raine.
Tom Milnes and Tony Palladino added another 39 for the eighth wicket before Milnes became the fifth Derbyshire batsman in the innings to be bowled, albeit off an inside edge.
Callum Parkinson, another making his first class debut, had already impressed with his left-arm spin, picking up four wickets in Leicestershire's first innings, but the 19-year-old showed he can also bat, giving Palladino good support as the last-wicket pair took the attack to the Leicestershire bowlers in the extra half hour.
Parkinson hit 32, from just 39 balls, as the 50 partnership was raised just before the break, and the pair extended the last wicket partnership to 73 before Palladino edged Raine to O'Brien.
Raine finished with 5 for 66, his best return of the season.
With their confidence boosted by their efforts with the bat, Derbyshire's seamers made early inroads. Milnes bowled Angus Robson in the first over for a duck with a full delivery, and Palladino had Paul Horton caught at second slip in the following over to leave the Foxes struggling on 1 for 2.
Neil Dexter and Mark Cosgrove added 29 for the third wicket before Dexter was given out leg before half forward to Milnes, umpire Rob Bailey deciding the ball had hit pad before bat, to reduce the Foxes to 30-3, a lead of just 48.
Mark Pettini took 24 balls to get off the mark, but together with Cosgrove then steadied the ship, adding 73 for the fourth wicket before Cosgrove, on 49, edged a delivery from Shiv Thakor which stayed a little low into his stumps.
No further runs had been added when Parkinson straightened one into Pettini as the right-hander stretched forward, and won the leg before decision.