An Egyptian court has ordered the release of deposed president Hosni Mubarak and his lawyer said he could leave Tora prison as soon as Thursday, given there are no legal grounds for any detention.

The court upheld a petition from Mubarak's lawyer, demanding the release of the man who ruled Egypt for 30 years and was removed amid a wave of popular protest in 2011.

Mubarak is being retried on charges of conspiracy to kill protesters but he has already served the maximum amount of pre-trial detention.

The 85-year-old was sentenced to life in prison last year for failing to stop the killing of protesters in 2011, but a court accepted his appeal and ordered a retrial. On Wednesday, the prosecutor cleared him in a corruption case.

He had been charged with squandering billions in public funds along with his two sons Alaa and Gamal, and illicitly redirecting state funds allocated for the renovation for presidential palaces towards private residences.

His release may cause further turbulence in Egypt, where the army ousted the country's first democratically elected president, Mohammed Morsi, on 3 July.

The Egyptian military-installed government, which has ordered the arrest of the Muslim Brotherhood general guide Mohamed Badie and other Islamist political figures, said they are fighting against terrorism.

But the release of Mubarak is underpinned by the conservative Gulf monarchies of Saudi Arabia and United Arab Emirates, which have promised Egypt $12 billion in aid since Morsi's removal.

Gulf kingdoms have always been angered by the detention of Mubarak, who they saw as a strong regional ally.