US District Judge Aileen Cannon, who was appointed to the bench by Trump, ruled that Special Counsel Jack Smith, who is leading the prosecution, was unlawfully appointed to his role and did not have the authority to bring the case.
The Justice Department said it would appeal the decision.
The Florida judge found that US Attorney General Merrick Garland, who named Smith in 2022 to oversee investigations involving Trump, did not have the authority "to appoint a federal officer with the kind of prosecutorial power wielded by Special Counsel Smith."
It marked another blockbuster legal triumph for Trump.
On July 1, the US Supreme Court ruled that Trump has broad immunity from criminal prosecution for official actions he took as president.
The ruling came in a separate case against Trump, also pursued by Smith, involving Trump's efforts to overturn his 2020 election loss.
Cannon's ruling came two days after Trump was the target of an assassination attempt at a campaign rally in western Pennsylvania. A bullet grazed his ear.
Trump, 78, is set to be formally named the Republican presidential nominee in Milwaukee this week, challenging 81-year-old Democratic President Joe Biden in the US election on November 5.
A spokesperson for Smith said Cannon's ruling "deviates from the uniform conclusion of all previous courts" that have considered the issue. Courts have consistently found the attorney general has the authority to appoint special counsels to handle certain investigations.
President Biden said he was "not surprised" by the case's dismissal. "It comes from the immunity decision the Supreme Court ruled on. The basis upon which the case was thrown out I find specious."
Trump, in a social media post, said the ruling should be "just the first step" and called for the dismissal of all four criminal cases against him.
Trump was convicted in May this year on New York state charges involving hush money paid to a porn star to avert a sex scandal before the 2016 election. He had pleaded not guilty in the documents case and in Smith's other case, as well as to election-related charges in state court in Georgia.
In the documents case, Trump was indicted on charges that he wilfully retained sensitive national security documents at his Mar-a-Lago estate in Florida after leaving office in 2021 and obstructed government efforts to retrieve the material. Prosecutors have said the documents related to US military and intelligence matters, including details about the American nuclear programme.
Trump personal aide Walt Nauta and Mar-a-Lago property manager Carlos De Oliveira were also charged with obstructing the investigation.
Prosecutors will seek to revive the case in their appeal before the Atlanta-based US Court of Appeals for the 11th Circuit.
Although half the judges on the 11th Circuit court were appointed by Trump, the court has not shied away from disagreeing with Cannon's opinions in the past.
In 2022, the court reversed Cannon's decision to appoint an independent "special master" to vet the documents seized by the FBI from Trump's Florida estate, saying Cannon lacked authority.
'BREATHTAKINGLY MISGUIDED'
US Senate Majority Leader Chuck Schumer, a Democrat, said in a statement: "This breathtakingly misguided ruling flies in the face of long-accepted practice and repetitive judicial precedence. It is wrong on the law and must be appealed immediately. This is further evidence that Judge Cannon cannot handle this case impartially and must be reassigned."
At the very least, Cannon's ruling throws the future of the case into doubt. Trump's lawyers have not made a similar challenge to the special counsel in Smith's election-related case.
Trump's lawyers challenged the legal authority for Garland's 2022 decision to appoint Smith to lead investigations into Trump. They argued that the appointment violated the US Constitution because Smith's office was not created by Congress and the special counsel was not confirmed by the Senate.
Lawyers in Smith's office disputed Trump's claims, arguing that there was a well-settled practice of using special counsels to manage politically sensitive investigations.
Cannon's ruling is the most consequential in a series of decisions she has made favouring Trump and expressing scepticism about the conduct of prosecutors. The judge previously delayed a trial indefinitely while considering a flurry of Trump legal challenges.
In an unusual move, she allowed three outside lawyers, including two who sided with Trump, to argue during a court hearing focused on Trump's challenge to Smith's appointment.
Conservative Supreme Court Justice Clarence Thomas provided a boost to Trump's challenge to the special counsel. In an opinion agreeing with the court's decision to grant Trump broad immunity, Thomas questioned whether Smith's appointment was lawful, using similar arguments to those made by Trump's lawyers.
Garland appointed Smith, a public corruption and international war crimes prosecutor, to give investigations into Trump a degree of independence from the Justice Department under Biden's administration.