The High Court ruling calls for South Africa's government to get parliamentary approval before withdrawing from the Hague-based court.
South Africa's decision to withdraw from the International Criminal Court (ICC) has been ruled invalid and unconstitutional by the country's High Court.
The country's main opposition party, the Democratic Alliance, had protested before launching court action to challenge the government's move.
The court said the executive needs parliament's approval before withdrawing from the Rome Statute – the treaty that established the ICC.
The ICC, which opened in July 2002 and has 124 member states, is the first legal body with permanent international jurisdiction to prosecute genocide, crimes against humanity, and war crimes.
Currently nine out of the ICC's 10 investigations concern African countries, the other being Georgia.
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.
Last October, South Africa decided to withdraw from the Hague-based ICC, arguing the court's rules were "in conflict and inconsistent with'' the country's diplomatic immunity law.
Immediately after the court ruling, the country's Justice Minister Michael Masutha said that the government would decide how to proceed, including a possible appeal, after reading the full judgment.
He said that South Africa still plans to withdraw from the ICC describing last year's notification to the UN of its intent as a policy decision.
Three African states - South Africa, Gambia and Burundi - last year signalled their intention to quit the ICC. Gambia's President Adama Barrow, elected in December, said earlier this month it will remain in the ICC.
South Africa said it was quitting the ICC because membership conflicted with diplomatic immunity laws.
Pretoria had in 2015 announced its intention to leave after the ICC criticised it for disregarding an order to arrest Sudanese President Omar Hassan al-Bashir, accused of genocide and war crimes, when he visited South Africa. Bashir has denied the accusations.