Derbyshire 368 (Guest 109, Madsen 70, Masood 60) and 170 for 2 (Madsen 58*, Masood 42, Guest 40*) lead Glamorgan 387 (Labuschagne 130, Lloyd 84, Lakmal 5-82) by 151 runs
For a while, it looked doubtful if Masood would even get the chance to break the record as Labuschagne and nightwatchman Timm van der Gugten batted through the first 20 overs of the day.
Derbyshire almost broke through early but Madsen at second slip failed to take a tough low catch when van der Gugten, on nine, edged Lakmal.
Labuschagne was beaten a couple of times by Ryan Sidebottom but the Australian played some sublime drives as he moved to his sixth hundred for Glamorgan from 137 balls
The stand was worth 82 from 22 overs when van der Gugten tried to hit leg-spinner Mattie McKiernan over the top but failed to clear long on.
Glamorgan’s prospects of a substantial lead disappeared with the second new ball which Lakmal and Sam Conners used skillfully to slow the scoring rate and build pressure.
Chris Cooke was lbw playing back to Conners and although Tom Cullen was dropped second ball at first slip, he played on to Lakmal in the second over after lunch.
Michael Neser got a leading edge, Lakmal trapped Labuschagne lbw and after some firm blows from the last-wicket pair, all eyes were on Masood.
He timed the ball effortlessly from the start, threading drives and clips off his legs through gaps to close in on Compton’s landmark.
The last over before tea began with Masood needing three runs to enter the record books but the first ball seamed away and lifted to take his outside edge and Cooke made no mistake.
Even so, Masood’s aggregate of 713 in five Championship innings is a remarkable statistic and he now has two more matches to get to the coveted milestone of 1,000 runs before the end of May.
With Masood back in the pavilion, it was now a question of how far ahead Derbyshire could get before stumps and Madsen came out intent on taking the attack to the bowlers.
James Harris replaced van der Gugten, who pulled up and left the field, and had Billy Godleman caught behind but was then savaged by Madsen who hit four boundaries in the over which cost 20.
Glamorgan restored a measure of control but Madsen reached his fifth consecutive score of 50 or more when he cut Neser for his seventh four.