BAGHDAD (Reuters) - Acting U.S. defense chief Patrick Shanahan told Iraqi leaders that Washington understood its forces were in Iraq at Baghdad’s invitation, aiming to steady relations after President Donald Trump said U.S. troops were needed to keep an eye on Iran.

Shanahan met Prime Minister Adel Abdul Mahdi during an unannounced trip to Baghdad on Tuesday.

“I made very clear that we recognize their sovereignty, their focus on independence and that we are there at the invitation of the government,” Shanahan told reporters.

Trump infuriated Iraqi politicians this month by saying that a U.S. presence was needed in Iraq to observe Iran. Washington, which occupied Iraq from 2003-2011, now has around 5,200 troops in Iraq at the request of the Baghdad government to help fight against Islamic State.

Iraq is an ally of both the United States and Iran, and its politics are often dominated by efforts to balance relations with two powerful countries that are bitter enemies.

Trump’s suggestion that Iran was the target of the U.S. presence in Iraq was denounced across the political spectrum in Baghdad last week, including by Abdul Mahdi. President Barham Salih said the U.S. troops must stick to their anti-Islamic State mission; Iran-aligned politicians in Iraq’s parliament pressed demands that the U.S. mission be scaled back.

The uproar came just months after Trump angered Iraqi leaders on his only visit to the country so far. On the day after Christmas Trump stopped only at a U.S. air base in the remote desert. Unlike predecessors Barack Obama and George W. Bush, he skipped the capital Baghdad and met no Iraqi officials.

FILE PHOTO - Acting U.S. defense secretary Patrick Shanahan arrives in Kabul, Afghanistan February 11, 2019. REUTERS/Idrees Ali

Shanahan said the topic of U.S. troops in Iraq and Trump’s Iran comments did not come up during his talks with Abdul Mahdi, but the prime minister told him that Iraq needed to maintain strong relations with both Washington and Tehran.

“There is activity in their legislature... the discussions have been should they restrict the number of U.S. forces in Iraq and I wanted to make clear to (Abdul Mahdi) that we recognize our role, we understand that we are there by invitation,” Shanahan said.

Speaking after meeting with Shanahan, Abdul Mahdi said he respected the decisions of the parliament.

“The government has its view and the political blocs have theirs,” he said.

SYRIA WITHDRAWAL

U.S. officials have criss-crossed the Middle East in recent months to reassure allies that Washington remains committed to the region, after Trump announced in December that he would abruptly withdraw the 2,000 U.S. troops from Syria.

That decision was opposed by top aides, including Defense Secretary Jim Mattis, who quit, leaving his deputy Shanahan in charge of the Pentagon.

Shanahan met with U.S. military commanders while in Iraq to get an update on the fight against Islamic State. U.S.-backed fighters have mounted an attack against Islamic State’s last bastion in eastern Syria.

U.S. officials have long estimated that the Syria pullout could take until sometime in March or April to execute fully, but have been reluctant to set an exact timeline given hard-to-predict battlefield conditions.

Islamic State was declared defeated in Iraq in 2017, but continues to wage small-scale insurgent attacks in some areas.