The US Justice Department has announced it will appeal against the ruling.
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 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".
Jack Smith did not have the authority to bring a case against Donald Trump, a judge has ruled. (AP PHOTO)
Cannon also found that Smith's investigation has been improperly funded through a permanent and unlimited fund Congress set aside in the 1980s for independent investigations.
Hours later, a spokesman for Smith, Peter Carr, said the Justice Department had authorised an appeal.
"The dismissal of the case deviates from the uniform conclusion of all previous courts to have considered the issue that the Attorney General is statutorily authorised to appoint a special counsel," Carr said in a statement.
The US Supreme Court ruled on July 1 that Trump cannot be prosecuted for actions that were within his constitutional powers as president - a landmark decision recognising for the first time any form of presidential immunity from prosecution.
That ruling involved charges pursued by Smith in a separate case against Trump in Washington DC involving his 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.
Trump has been formally named the Republican presidential nominee at the party's convention in Milwaukee, to challenge Democratic President Joe Biden in the November 5 election.
Courts in other cases have repeatedly upheld the ability of the US Justice Department to appoint special counsels to handle certain politically sensitive investigations.
Trump, in a social media post, said Cannon's ruling should be a "just the first step" and called for the dismissal of all four criminal cases against him.
"Let us come together to END all Weaponisation of our Justice System," Trump wrote.
Donald Trump had been indicted on charges that he kept national security documents. (AP PHOTO)
Trump was convicted in May on New York state felony charges involving hush money paid to a porn star to avert a sex scandal before the 2016 election.
Trump 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 country's nuclear program.
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.