LONDON — The U.S.-led coalition fighting the Islamic State said Monday that it will move hundreds of troops out of bases in Iraq, with most redeployed to other locations in the country, as threats from the militants wane and risks to foreign troops elsewhere in Iraq increase.

The coalition said that troops would be leaving the al-Qaim base in western Iraq within days and that they would soon move out of the northern K1 and Key West bases, too, with most heading to larger bases in Iraq and others to Syria or Kuwait.

The move, officials said, has been planned for months and is not the result of a wave of attacks by Iran-backed militia groups, which have killed and wounded almost two dozen coalition troops in recent weeks.

“As a result of the success of Iraqi Security Forces in their fight against ISIS, the Coalition is repositioning troops from a few smaller bases,” Col. Myles B. Caggins III, a spokesman for the anti-Islamic State mission. “These bases remain under Iraqi control and we will continue our advising partnership for the permanent defeat of Daesh from other Iraqi military bases,” he said, using an Arabic acronym for the militants.

Another U.S. official, speaking on the condition of anonymity because of the sensitivity of the situation, said planning for the move began in the fall, after military officials judged that the threat from Islamic State forces had diminished across swaths of Iraq.

But as danger from the militants had waned, tensions with Iran had escalated, and the more than 5,000 U.S. troops serving in Iraq as part of the anti-Islamic State coalition are now in the crosshairs of Iraqi militia groups backed by Tehran. Militia rocket strikes on bases hosting coalition troops have become a regular occurrence and have already brought the United States and Iran to the brink of war once this year.

Last week in Iraq, the United States launched its second wave of airstrikes targeting the Iran-backed Kataib Hezbollah group in the space of three months, after one British and two American service members were killed in suspected militia rocket attacks on Camp Taji, an Iraqi military base north of Baghdad.

The U.S. Defense Department said the airstrikes had targeted five weapons storage facilities linked to Kataib Hezbollah to “degrade” its ability to launch future strikes. But a day later, Camp Taji was hit by another salvo of rockets, wounding two Iraqi and three U.S. service members.

Iraqi miliary photos showed a row of unused, apparently ­Iranian-made, 107mm ­Fadjr-1 rockets left behind in the area where the munitions were fired from, weapons researchers said.

A newly announced group calling itself Usbat al-Thairen, or the League of Revolutionaries, on Sunday asserted responsibility for the strike.“We rejoice in the fear that we have struck in the chests of the occupying enemy as a result of our quality operations,” the group said in a video statement circulated on Telegram. “This operation is only the beginning and the end is soon to come.”

U.S. Secretary of State Mike Pompeo discussed the attacks with Iraq’s caretaker prime minister, Adel Abdul Mahdi, during a phone call Sunday, according to a statement. “Secretary Pompeo underscored that the groups responsible for these attacks must be held accountable,” it said, adding that the United States would “not tolerate attacks and threats to American lives.”

The U.S.-led coalition mission in Iraq began in 2014 after Islamic State militants swept across a third of the country and seized swaths of neighboring Syria as well. Six years on, the extremist group has been reduced to hiding out across rugged terrain on the periphery of state control in Iraq and Syria.

In January, the coalition was forced to temporarily suspend operations against the Islamist militants in Iraq, after President Trump’s decision to kill renowned Iranian general Qasem Soleimani set off a firestorm of condemnation. Iraq’s parliament has urged foreign troops to leave.

As tensions spiked with Iran earlier this year, the coalition suspended most operations against the Islamic State. But U.S. officials said several unannounced operations took place, reflecting how important coalition airstrikes and intelligence support remain in the fight against the group. By the end of January, official cooperation had resumed.

Two members of a Marines Special Operations team were killed this month during a mission targeting what officials described as an Islamic State mountain hideout.