BAGHDAD — Prime Minister Nuri Kamal al-Maliki said Thursday night that he had agreed to relinquish power, a move that came after days of crisis in which his deployment of extra security forces around the capital had raised worries of a military coup.

Mr. Maliki’s decision held out the prospect of a peaceful transition of power, based on democratic elections and without the guiding hand of American military forces, which would be a first in modern Iraq’s troubled history of kings, coups and dictatorships.

His decision to step aside came after heavy pressure from the United States, which has deployed warplanes in Iraq to target Sunni Islamist militants and suggested that more military support would be forthcoming if Mr. Maliki was removed from power. Iran also played a decisive role in convincing Mr. Maliki that he could not stay in power.

Mr. Maliki, 64, agreed to end his legal challenge to the nomination of his replacement, Haider al-Abadi, 62, a member of Mr. Maliki’s own Dawa Party, who was chosen Monday by Iraq’s president.

On state television, standing next to Mr. Abadi and other party leaders and reading from a prepared text, Mr. Maliki said, “I announce before you today, to facilitate the political process and to form a new government, that I withdraw my candidacy in favor of the brother Dr. Haider al-Abadi, and all that goes with that in order to preserve the high interests of the country.”

During the 12-minute speech, Mr. Maliki portrayed himself as a loyal leader who had used all the means at his disposal to defend Iraq from terrorism and what he characterized as internationally backed plots to weaken and divide his government.

Now that Mr. Maliki is leaving, the crucial question, given Iraq’s many divisions, is if Mr. Abadi, a Shiite like Mr. Maliki, can rise above sectarianism and be a truly national figure. He will try to stitch together a national unity government that establishes a new political bargain between Iraq’s three main communities — the majority Shiites and the minority Sunnis and Kurds. The United States has insisted on a political solution so Iraq’s leaders can present a united front against the onslaught by the Islamic State in Iraq and Syria, which has seized control of large portions of northern and western Iraq.

Video

President Obama spoke about the continuing military operation in Iraq and the improved situation on Mount Sinjar.CreditCreditNicholas Kamm/Agence France-Presse — Getty Images

Many of the Sunnis and Kurds had become so disenchanted with Mr. Maliki that they were willing to accept any new Shiite leader. Still, Mr. Abadi will have to make real concessions if he is going to persuade Sunnis and Kurds to commit to the political process in Baghdad. Mr. Abadi has been a member of Parliament, and after the American invasion he was briefly communications minister.

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.

For weeks at Friday Prayer, representatives of the clerics have made veiled calls for political change. Then, this week, the ayatollah reportedly sent a handwritten letter to Mr. Maliki that made a more direct call for him to leave power.

The unexpected timing of the announcement — late Thursday night — was partly aimed at avoiding another Friday of calls from the mosques for his departure, said one Shiite official from Mr. Maliki’s bloc, State of Law, who spoke anonymously because of the sensitivities of the discussions. “He lost everyone, even his closest aides,” the official said. “He wanted to save face.”

Mr. Abadi, according to the Constitution, has 30 days from the time of his appointment on Monday to form a new government. Mr. Maliki will remain as a caretaker prime minister until then.

In an interview Thursday morning, Ali al-Alaq, a Dawa Party leader who for decades worked with Mr. Maliki and Mr. Abadi in exile to oppose Saddam Hussein’s government, said that party members had been holding meetings, urging Mr. Maliki to step aside.

“The mission in Dawa is to peacefully transfer power between Maliki and Abadi,” Mr. Alaq said.

In a statement Thursday evening, Susan E. Rice, President Obama’s national security adviser, praised Mr. Maliki for his decision and said the United States was encouraged by the progress Iraqis had been making in building a new government.

“We have heard from a wide range of leaders across the Iraqi political spectrum who have expressed their commitment to work with Dr. Abadi to form a broad, inclusive government with an agenda that can address the needs and legitimate aspirations of the Iraqi people,” Ms. Rice said.

Mr. Maliki rose to power in 2006 with American support, and earned a second term in 2010, again with American backing that came after a failed effort at replacing him. But he had become an increasingly divisive figure. In particular, his policies toward the Sunnis — arresting leaders, the unlawful mass detentions of young Sunni men in the name of fighting terrorism — alienated the community, driving many from politics and toward sympathy with armed rebellion.

His resistance to Kurdish efforts to sell their own oil drove the Kurds, who already enjoy a great deal of autonomy in the north, further from the center of Iraqi politics in Baghdad.

In acknowledging his defeat at the hands of his opponents, Mr. Maliki characterized the rise of ISIS as the work of foreign powers that threatened the entire country. He said, “O people of Iraq, understand well that this is not just Maliki who is a target, but all of Iraq.”

Reporting was contributed by Suadad al-Salhy, Ben Hubbard and Omar Al-Jawoshy from Baghdad, and Michael D. Shear from Edgartown, Mass.

A version of this article appears in print on , Section A, Page 1 of the New York edition with the headline: Premier of Iraq Accedes to Calls to Give Up Power. Order Reprints | Today’s Paper | Subscribe