President Vladimir V. Putin will not attend a diplomatic summit in Johannesburg next month, South Africa’s president announced on Wednesday, a decision that allows the host nation to avoid the difficult predicament of whether to arrest the Russian leader, who is the subject of an international warrant.

President Cyril Ramaphosa of South Africa had said in a court affidavit made public on Tuesday that his country would risk war with Russia if it arrested Mr. Putin at the summit.

The decision for Mr. Putin not to attend was made “by mutual agreement,” according to a statement released by Mr. Ramaphosa’s office. Russia will instead be represented by its foreign minister, Sergey V. Lavrov, the statement said.

The news caps a tense few months for South African officials, who painstakingly deliberated over how to proceed, given that their government considers Russia a close friend and ally. South African officials were forced to weigh that alliance against its relationship with Western partners, which has been strained lately because of South Africa’s refusal to condemn Russia’s war in Ukraine.

Mr. Ramaphosa’s court affidavit was the clearest indication yet that South Africa was seeking any way possible to avoid arresting Mr. Putin when it hosts a long-planned meeting of the heads of state of Brazil, Russia, India, China and South Africa, a bloc known as BRICS.

Mr. Putin is the subject of an arrest warrant on accusations related to the war in Ukraine by the International Criminal Court. The warrant makes South Africa, as a signatory to the court, legally obliged to arrest the Russian president. Russia “has made it clear” that arresting Mr. Putin “would be a declaration of war,” Mr. Ramaphosa said in his affidavit.

“It would be inconsistent with our Constitution to risk engaging in war with Russia,” Mr. Ramaphosa wrote in the 32-page affidavit.

Mr. Ramaphosa was responding to a petition by South Africa’s largest opposition political party, the Democratic Alliance, that asked a court in Pretoria, the nation’s executive capital, to force the government to arrest Mr. Putin if he attended the summit, in Johannesburg, in late August. The court is expected to hear arguments in the case on Friday.

Mr. Ramaphosa argued in his affidavit that South Africa’s Bill of Rights required the government to protect and promote certain rights, including “the right to be free from all forms of violence.”

“An act that would be perceived as a declaration of war by Russia would be reckless,” Mr. Ramaphosa wrote, and conflict with his and “the government’s constitutional obligations.”

Mr. Ramaphosa also argued that arresting Mr. Putin would conflict with South Africa’s effort to broker a peace deal between Russia and Ukraine. Mr. Ramaphosa joined several African leaders last month in meeting with President Volodymyr Zelensky of Ukraine in Kyiv and then with Mr. Putin in St. Petersburg, Russia, to discuss a path to ending the war — a mission that was met with skepticism from both.

South Africa has been exploring options that would allow it to avoid arresting Mr. Putin if he goes to Johannesburg. Mr. Ramaphosa said in his affidavit that he was consulting with the leaders of each BRICS country, and he asked the court to give him time to complete the consultation.

Last week, South Africa’s deputy president, Paul Mashatile, said his country had raised the possibility of holding the summit virtually or moving it to China. Both options were rejected by South Africa’s BRICS partners, he said. And Russian officials have resisted a suggestion that Mr. Putin’s foreign minister attend the summit in his place, Mr. Mashatile said.

The summit is scheduled to be held from Aug. 22 to Aug. 24.