NASCAR officials had banned Mayfield in May because of what they said was a positive test for methamphetamine.
"If he is allowed to race again while this litigation is pending, it's possible he'll end up hurting or killing someone," NASCAR attorney Helen Maher told The Charlotte Observer.
Mayfield denied using the drug and sued NASCAR, which filed a countersuit. Mayfield said he was unable to have an independent test of the "B" sample and his attorneys argued that Mayfield showed no symptoms of a methamphetamine user.
U.S. District Judge Graham Mullen did not rule on the possibility that the drug test was botched, an issue that could eventually be resolved by a jury.
Mullen said, however, the driver "had sufficiently alleged irreparable harm." He said if NASCAR believes Mayfield has a drug problem, the auto racing organization can test him.
NASCAR alleges Mayfield was in three wrecks in Sprint Cup races this season while drugs were in his system.
Martin Bashir resigns from MSNBC over Sarah Palin comments
Florida bear attack: Black bear mauls woman's face