The Saudi Embassy in Baghdad, shut for 25 years, had reopened on Friday, a day before the diplomatic feud.

After Sheikh Nimr was executed, some Iraqi Shiite groups close to Iran quickly demanded that it be closed. At one point, protesters gathered near the fortified Green Zone, where the Saudi and other embassies are.

But Mr. Abadi decided that closing the Saudi Embassy was “not in the interests of Iraq,” said Mr. Hadithi, his spokesman, adding, “The Iraqi government preserves its relations” with the countries of the world, he said, “and that includes Saudi Arabia.”

Mr. Abadi did issue a statement expressing “great sorrow and sad shock” at the execution, and criticizing what he called Saudi Arabia’s “mouth-muzzling policy.” He called free expression of opinions and peaceful opposition “basic rights,” and said that violating them would lead to insecurity and instability.

Mr. Abadi’s main task in office has been to maintain crucial ties with Shiite forces, often backed by Iran, which have proved more effective than the army in the fight against the Islamic State, while also restoring ties with the Sunni minority. Many Sunnis’ sense of neglect by the Shiite-led government in Baghdad helped create the opening for the Islamic State to seize much of the north and west of Iraq in 2014.

Most analysts believe he has done a decent job of this, at least better than his predecessor, Nuri Kamal al-Maliki, who presided over discriminatory policies that alienated many Sunnis. His challenge now, as he looks north to the greater challenge of trying to retake Mosul, Iraq’s second-largest city, is to retain the support of Sunni tribes.

Tensions flared last summer after Shiite forces helped oust the Islamic State from Sunni areas like the city of Tikrit. There were reports that Shiite fighters forced Sunnis from their homes and looted their property. But by contrast, Ramadi was taken by a combination of Iraqi security forces and armed Sunni fighters from the area — with the help of heavy American airstrikes, which had been less forthcoming when Iranian-backed militias led the fight in Tikrit.