Spain's new prime minister Jose Luis Rodriguez Zapatero has given orders for Spanish troops in Iraq to be brought home in "as short a time as possible".
Spain has about 1,300 troops stationed in Iraq
In a televised address to the nation, he said he could not ignore what he called the will of the Spanish people.
Spain's foreign minister told his Egyptian counterpart the pull-out would be "within 15 days", the Egyptian foreign ministry said in a statement.
Spain has about 1,300 troops stationed in southern central areas of Iraq.
The BBC's Katya Adler, in Madrid, says military sources there say the pull-out is more likely to take one or two months because of operational requirements.
Meanwhile, the top US administrator in Iraq warned the country's police and armed forces would not be able to secure the country against insurgents by the time the US hands over power to an Iraqi government on 30 June.
Paul Bremer's comments - aimed to defend the continued presence of US troops in Iraq after the occupation officially ends - came as 10 US troops died in clashes with insurgents or other combat across Iraq at the weekend.
A tense stand-off is also continuing in the holy city of Najaf between US forces and supporters of radical Shia cleric Moqtada Sadr.
"Events of the past two weeks show that Iraq still faces security threats and needs outside help to deal with them," Mr Bremer said.
'No UN mandate'
Mr Zapatero said he had ordered the defence minister to "do what is necessary for the Spanish troops stationed in Iraq return home in the shortest time possible".
He spoke just hours after the new Socialist government was sworn in.
Mr Zapatero's election win last month was largely unexpected
The previous government's support for the war in Iraq, and its handling of the Madrid bombings, were thought to have caused its election downfall.
Immediately after his election, Mr Zapatero had vowed to withdraw Spanish troops from Iraq unless they came under UN command by 30 June when their mandate expires.
His conservative predecessor, Jose Maria Aznar, sent in troops in August 2003 in a move which led to huge popular protests across Spain.
"With the information we have, and which we have gathered over the past few weeks, it is not foreseeable that the United Nations will adopt a resolution" that satisfies Spain's terms, Mr Zapatero said.
The United States earlier condemned the decision to bring the troops home, saying it is giving in to terrorism.
MAIN FOREIGN TROOPS IN IRAQ
But on Sunday the White House said the US would continue close co-operation with Spain in the war against terror.
A spokesman said the US expected Spain would "implement their decision in
a co-ordinated, responsible and orderly manner".
The BBC's Danny Wood, in Madrid, says the majority of Spaniards support the move.
Many believe the 11 March train bomb attacks were a result of the former government's support of the US policy in Iraq, our correspondent adds.
Spanish troops play an important role in Iraq, particularly in the Shia holy city of Najaf.
Spanish troops stationed in Najaf come under rifle and mortar fire almost every day from militiamen loyal to the radical anti-US cleric Moqtada Sadr.
In November, Spain was stunned by the deaths of seven intelligence officers in Iraq, killed when their convoy came under attack outside Baghdad.
The BBC's Dominic Hughes, in Baghdad, says that with the US saying it needs thousands of extra troops on the ground in Iraq, the withdrawal of the Spanish forces will clearly be felt.
Mr Zapatero has said the fight against terrorism at home and abroad will be his administration's top priority.
And he has promised to double the number of Spanish soldiers in Afghanistan, where there is a UN mandate.