The ICC has been rocked by threats of withdrawal in recent months, with complaints focusing on its alleged bias against Africa.
South Africa announced in October it had lodged its decision to pull out with the United Nations, following a dispute over Sudanese President Omar al-Bashir visiting the country in 2015.
South African authorities refused to arrest Bashir despite him facing an ICC arrest warrant over alleged war crimes, saying he had immunity as a head of state.
The president and ministers "are ordered forthwith to revoke the notice of withdrawal," he added.
More From This Section
The court ruling was a setback for President Jacob Zuma's government, which could appeal.
The opposition Democratic Alliance (DA) party, which was one of the groups that brought the court case, welcomed the judgement.
"The withdrawal by the South African government from the ICC was irrational and unconstitutional," DA lawmaker James Selfie told AFP.
"We would like South Africa to stay in the ICC because we believe that it is consistent with our constitution and with the legacy of Nelson Mandela.
After the election of President Adama Barrow, The Gambia's new government in February asked the UN to halt its process of withdrawal from the ICC.
Burundi has also registered to leave, while Kenya is considering the move.
President Bashir has evaded arrest since his ICC indictment in 2009 for alleged war crimes in Sudan's Darfur conflict in which 300,000 people were killed and two million forced to flee their homes.
South Africa's Supreme Court of Appeal accused the government of "disgraceful conduct" over Bashir's visit and ruled that the failure to arrest the Sudanese leader was unlawful.
However experts point out that many of the current investigations -- in the Central African Republic, Uganda, Mali and the Democratic Republic of Congo -- were referred to the ICC by the governments of those states.