A South African court on Wednesday ruled the government's plan to withdraw from the International Criminal Court (ICC) was "unconstitutional and invalid", dealing a blow to President Jacob Zuma.
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 withdraw with the United Nations, following a dispute over Sudanese President Omar al-Bashir visiting the country.
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 cabinet decision to deliver the notice of withdrawal to the United Nations secretary general without prior parliamentary approval is unconstitutional and invalid," said judge Phineas Mojapelo in the North Gauteng High Court.
The president and ministers "are ordered forthwith to invoke the notice of withdrawal," he added.
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.
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.