Former President Jacob Zuma of South Africa returned to prison early Friday morning to continue serving a sentence for contempt, but was released almost immediately under a program to relieve overcrowding in the country’s jails, the authorities said.

With his release under the program, it is unlikely that Mr. Zuma will serve more time in prison on the contempt charge. Political opponents accused the government, run by Mr. Zuma’s political party, of giving him preferential treatment, saying it had intentionally started to roll out the overcrowding program on the day he reported to jail.

Mr. Zuma had served just two months of a 15-month sentence in 2021 for defying a court order to testify before a national inquiry on corruption when he was released on medical parole by the corrections commissioner at the time, a close political ally.

But last year, an appeals court ruled that Mr. Zuma’s release was unlawful and that he had to return to prison to serve the remainder of his sentence, a decision upheld by the country’s highest judicial body last month.

The current corrections commissioner, Makgothi Thobakgale, said he had complied with the court’s order by ordering Mr. Zuma to return to a rural prison in KwaZulu-Natal Province. The former president’s immediate release on Friday stemmed from a “remission” program approved by the current president, Cyril Ramaphosa, that will allow for the release of nearly 9,500 inmates serving time for nonviolent offenses to ease prison overcrowding, the authorities said.

South African justice officials had been working out the details of the remission program since April, according to Ronald Lamola, the justice minister. But it was not until Friday, when Mr. Zuma reported to prison at about 6 a.m., that the program was put in place, meaning that Mr. Zuma was among its first beneficiaries.

The Democratic Alliance, South Africa’s largest opposition party, criticized the decision to release Mr. Zuma, saying that the remission program’s start on the same day he reported to prison was no coincidence.

“This is a monumental insult to each and every South African,” Glynnis Breytenbach, a member of Parliament for the Democratic Alliance overseeing the justice ministry, said in a statement. “This entire scheme was devised with one aim in mind — to ensure that Zuma does not spend any time in prison.”

Mr. Lamola rebuffed suggestions in a news conference that Mr. Zuma had received special treatment. He said the program was implemented only when it was ready, which was on the same day Mr. Zuma was set to return to prison. The release of thousands of other inmates will proceed over the next 10 months, he said.

Mr. Thobakgale said that Mr. Zuma, 81, was in a category of low-risk, vulnerable inmates including older people and pregnant women who could be released quickly. Mr. Zuma left the Estcourt Correctional Center within two hours of arriving on Friday morning.

“The law has taken its course with regards to the former President Jacob Zuma,” Mr. Lamola said, adding that he believed his release would withstand any court challenges.

Mr. Lamola said that neither he nor Mr. Ramaphosa, a rival of Mr. Zuma’s within the governing African National Congress, had interfered with the correctional commissioner’s decision to return the former president to prison. The remission process was happening on a parallel track, Mr. Lamola said.

He also said that releasing Mr. Zuma had nothing to do with avoiding violence. When Mr. Zuma was sent to prison in July 2021, his supporters took to social media to call for protests. Those demonstrations eventually grew to include South Africans with broader grievances over the country’s difficult economic conditions. They spiraled out of control into what became some of the worst post-apartheid unrest in South Africa, with widespread looting, the destruction of buildings and the deaths of more than 300 people.