Protesters attacked the US Embassy in Baghdad on Tuesday and the ambassador and other staff were evacuated as anger over US airstrikes on Iraq erupted into violence.
In Washington, US President Donald Trump accused Iran of orchestrating the unrest and said Tehran would be held responsible.
The protesters and militia fighters stormed and burned a security kiosk at the entrance of the US Embassy but did not breach the main compound as security forces and embassy guards fired tear gas and stun grenades, Reuters witnesses said.
They threw stones at the gate while others chanted, "No, no, America! No, no, Trump!"
Iraqi special forces were deployed around the main gate to prevent them entering the embassy. US-trained and -equipped Iraqi Counter-Terrorism forces later reinforced them.
The US planes on Sunday had attacked bases belonging to an Iranian-backed militia - an action that risks drawing Iraq further into a proxy conflict between Washington and Tehran at a time when mass protests are challenging Iraq's political system.
The attack on the Kataib Hezbollah militia was in response to the killing of a US civilian contractor in a rocket attack on an Iraqi military base.
"Iran killed an American contractor, wounding many. We strongly responded, and always will," Trump said in a tweet.
"Now Iran is orchestrating an attack on the US Embassy in Iraq. They will be held fully responsible. In addition, we expect Iraq to use its forces to protect the Embassy, and so notified."
The two Iraqi foreign ministry officials said the US ambassador and other staff had left, but they did not say when. A few embassy protection staff remained, they said.
A few hours into the protest, tear gas was fired in an attempt to disperse the crowd and some of the militias encouraged protesters through loudspeakers to leave.
Security guards inside the embassy also fired stun grenades at protesters outside the gates of the compound. Reuters correspondents heard at least seven loud bangs.
"We have delivered our message, please leave the area to avoid bloodshed," one announcement said.
'CLOSED IN THE NAME OF THE PEOPLE'
Iraqis have been taking to the streets in their thousands almost daily to condemn, among other things, militias such as Kataib Hezbollah and their Iranian patrons that support Iraqi Prime Minister Adel Abdul Mahdi's government.
But on Tuesday, the militias sprayed "Closed in the name of the people" on the gates of the US Embassy and smashed the surveillance cameras around the building with bricks and stones. Some set up tents in preparation for a sit-in.
Qais al-Khazali, leader of the Iranian-backed Asaib Ahl al-Haq militia, and many other senior leaders were among the protesters.
"Americans are unwanted in Iraq. They are a source of evil and we want them to leave," Khazali told Reuters.
Khazali is one of the most feared and respected Shi'ite militia leaders in Iraq, and one of Iran's most important allies.
Kataib Hezbollah is one of the smallest but most potent of the Iranian-backed militias. Its flags were hung on the fence surrounding the embassy.
Militia commander Jamal Jaafar Ibrahimi, also known as Abu Mahdi al-Mohandes, and Badr Organisation leader Hadi al-Amiri were also at the protest.
Abdul Mahdi has condemned the airstrikes, which killed at least 25 fighters and wounded 55.