Organised by trade unions and social groups on Sunday, the protest comes a year after massive demonstrations in the capital and Barcelona against war.
Spanish PM Jose Maria Aznar, whose right-wing Popular Party is seeking re-election next month, was one of the staunchest supporters of the US-led invasion and occupation of Iraq.
Organisers said About 150,000 people took part in Sunday's march, but local officials put the figure at about 9000.
The protesters, who carried banners demanding "USA out" and "Popular Party go", found support from members of Spain's opposition Socialist Party (PSOE) and the Communist-led United Left coalition (IU).
IU leader Gaspar Llamazares appealed in comments to journalists for "the immediate return of Spanish troops" and the end "of an occupation that is totally illegitimate [and] which prompted a civil war in Iraq".
He urged Spaniards to reject government "lies" about Iraq's alleged weapons of mass destruction in the general election on 14 March.
Elsewhere in Spain
Several thousand people also took to the streets in the eastern cities of Barcelona and Valencia.
In the south, only a few hundred turned out in the southern city of Seville, while about 1000 demonstrated in Tarragona.
Writer Rosa Regas and actor Juan Diego Botto read a statement at the end of the rally in Madrid calling for international troops to be withdrawn from Iraq and demanding the return of "sovereignty and self-determination to the Iraqi people".
Spain currently has almost 1300 soldiers serving in Iraq. They were deployed by Aznar's government last August in the face of massive political and public opposition.