South Africa’s decision to withdraw from the International Criminal Court (ICC) has been ruled "unconstitutional and invalid" by the High Court.
South Africa notified the UN of its intention to leave last October, saying the ICC pursued "regime change".
The court ruled in favour of the opposition Democratic Alliance (DA), which argued that the government had to first seek parliamentary approval.
The court ordered the government to revoke its notice of withdrawal.
In his response, Justice Minister Michael Masutha said the government still intended to quit the ICC, Reuters news agency reports.
The government would consider its options, including a possible appeal, after studying the full judgement, he is quoted as saying.
The decision to pull out came after a dispute over Sudanese President Omar al-Bashir’s visit to the country in 2015.
South African authorities refused to arrest Mr Bashir despite him facing an ICC arrest warrant over alleged war crimes.
Mr Bashir was attending an African Union summit in Johannesburg, when the government ignored an ICC request to arrest him.
The DA welcomed the judgement.
"South Africa does not want to be lumped together with pariah states who have no respect for human rights and who do not subscribe to accountability for those guilty of the most heinous human rights violations," the party said in a statement.
"Instead, we should recommit our country to the human rights-based foreign policy spearheaded by the late President Nelson Mandela," the statement added.
Another defeat for Zuma: Andrew Harding, BBC News, Johannesburg
The High Court’s decision marks a pause, rather than a full stop, for the South African government and its plan to withdraw from the International Criminal Court.
The government may choose to appeal the judgment, or it may simply do as the judges ordered and take the proposal to parliament where the governing African National Congress (ANC) continues to enjoy a comfortable majority.
But today’s emphatic judgment against the government is, nonetheless, another uncomfortable defeat for President Jacob Zuma’s team, which has now lost three times in a row in legal challenges related to the ICC.
Some ANC officials have frequently criticized the judiciary for "interfering" in what they argue are political matters, and attacked opposition parties and NGOs for using the courts to thwart their popular mandate.
Although there are signs that many African governments are beginning to lose faith in an ICC that they believe is targeting their continent unfairly and disproportionately, there is no clear consensus on the issue - let alone any signs of significant public concern.
The court ruling in South Africa is a reminder that the slow, exhaustive process of signing up to the Rome Statute, which set up the ICC, cannot simply be reversed at the stroke of a pen.
South Africa, Burundi and Namibia are among African states that have said they will withdraw from the ICC. They accuse the court of bias against Africans.
The Gambia, which had also announced its withdrawal, has now said that it will remain in the ICC.
This follows a change of government in the West African state.