The legislature also called for the government to prevent any foreign troops from using Iraqi lands or airspace for any reason.
The move comes after a US airstrike in Baghdad killed Qassem Soleimani, the commander of Iran's elite Quds Force, and Abu Mahdi al-Mohandes, the deputy head of Iraq's Muslim Shiite militia Hashd al-Shaabi, along with several other Iran-allied militiamen.
The strike was condemned by Iraqi officials and caused anger among Iraqi people.
Soleimani was considered one of Iran's most influential military leaders, wielding influence in Iraq, Syria and other parts of the Middle East where Iran has a foothold.
The parliament also demanded that Iraq's foreign minister lodge a complaint to the United Nations against the US due to "dangerous breaches of the sovereignty and security of Iraq."
Under the resolution, the Iraqi government will determine the numbers of foreign military technicians and trainers it needs, their locations and the periods of their contracts.
Some 168 lawmakers out of the 329-strong parliament attended Sunday's session.
Iraq's caretaker prime minister Adel Abdel-Mahdi said steps were being worked out for the departure of foreign troops from the country, the official Iraqi news agency INA reported.
"Iraqi officials in different departments are preparing a memorandum on the legal and procedural steps to implement the parliament’s decision on the foreign troops' withdrawal," Abdel-Mahdi's office said in a statement, without giving details.
Addressing parliament earlier on Sunday, Abdel-Mahdi said the activities of foreign troops in Iraq should be limited to training, not combat missions.
"We have two options: to immediately end this foreign presence or to set a timetable for this," he added.
The US was "disappointed" by the Iraqi parliament's action, a State Department spokesperson said.
"While we await further clarification on the legal nature and impact of today's resolution, we strongly urge Iraqi leaders to reconsider the importance of the ongoing economic and security relationship between the two countries and the continued presence of the Global Coalition to Defeat ISIS," Morgan Ortagus said in a statement.
"We believe it is in the shared interests of the United States and Iraq to continue fighting ISIS together," she said, adding that the Trump administration remains "committed to a sovereign, stable, and prosperous Iraq."
The US-led global coalition against Islamic State was formed in September 2014.
Anti-US sentiment has surged in Iraq in the wake of the airstrike that killed Soleimani and al-Mohandes.
Later on Sunday, two Katyusha rockets landed inside Baghdad’s heavily fortified Green Zone, home to the US embassy and other foreign diplomatic missions, a government security media centre said without reporting casualties.
This week, thousands of supporters of Hashd al-Shaabi broke into the US embassy compound in central Baghdad in protest against recent deadly US airstrikes in Iraq and Syria that targeted the Iran-backed Kataib Hezbollah militia, a Hashd faction.
------------------------------------------------------------------------------------------------------