Mr. Maliki’s decision came after days of negotiations with his former Shiite allies, who urged him to give up in the face of growing international opposition to his rule and the sense among most Iraqi leaders that his removal was necessary to bring the country together as it fights against a growing insurgency led by ISIS.

Officials said that in days of negotiations over his future, Mr. Maliki was given assurances — although not in a formal agreement — that he would be protected from prosecution. He is also expected to take a post in a new government, and while the position of vice president has been discussed, the matter has not been settled. Mr. Maliki was also assured that members of his bloc — which won the most seats in April’s national elections — would be given their fair share of ministries and other positions.

Officials said that as the dispute grew this week between Mr. Maliki and other Shiite leaders, Iran stepped in to take a greater role, one that proved decisive. Iran has been a strong supporter of Mr. Maliki in the past, but its main goal in Iraq is Shiite unity.

“The Iranians played a great role in this reconciliation process between Dawa leaders,” said a Shiite leader involved in the talks, who spoke on the condition of anonymity to discuss confidential matters. “There were semi-daily messages and meetings the last few days to convince Maliki that he has to step down in an honorable way.”

In his speech, Mr. Maliki said that he sought no particular position, but that he would remain “a fighting soldier defending Iraq and its people.”

In addition to the Americans and Iranians, another deciding factor in Mr. Maliki’s decision was opposition to his rule by Grand Ayatollah Ali al-Sistani, the most important Shiite cleric in the world, who lives in the holy city of Najaf, in southern Iraq. By tradition, Iraq’s Shiite clerics keep a distance from politics, but the intervention by Ayatollah Sistani and other clerics in the efforts to remove Mr. Maliki underscored the gravity of the crisis.