The ruling came after attorneys for Martinelli, who stands accused of spying on his political opponents, argued he should be released as Panamanian law bars pre-trial detention beyond one year.
Judge Raul Vergara agreed, saying "the court orders the cessation of the precautionary measure imposed on Ricardo Martinelli" and places him under house arrest.
Prosecutors have lodged an appeal, arguing the ex-president was a flight risk.
In office from 2009 to 2014, supermarket tycoon Martinelli was extradited from the US in June 2018 after he fled to Miami in 2015 to avoid arrest.
Prosecutors accuse him of graft and spying on his political foes, as well as helping embezzle $45 million from a government school lunch program. They are seeking a 21-year sentence.
Martinelli denies the accusations, claiming he is a victim of "political persecution" by the government of outgoing President Juan Carlos Varela, his former political ally.
About fifty Martinelli supporters cheered the decision outside the court, shouting "freedom!" "freedom!"