Updated

Spain's ruling conservatives crashed to defeat in elections overshadowed by anger over terrorist bombings, becoming the first government that backed the U.S.-led war in Iraq to be voted out of office.

The surprise win by the Socialists over Prime Minister Jose Maria Aznar's (search) favored Popular Party Sunday came amid charges that Aznar made Spain a target for terrorist by supporting the Iraq war.

Spain's incoming prime minister, Jose Luis Rodriguez Zapatero (search), has pledged to bring home the 1,300 Spanish troops in Iraq when their tour of duty ends in July.

Zapatero fell short of a majority in Parliament and will need help to form a government. But it was still a spectacular — and bittersweet — triumph that capped four tumultuous days beginning with Thursday's terror attacks in Madrid (search), which killed 201 people and wounded 1,500. Critics of the government said it had provoked the attacks by backing the Iraq war.

Next came millions-strong, nationwide street rallies against the railway bombings, smaller ones against the increasingly beleaguered government of Aznar, the arrest of five suspects in the bombings, including three Moroccans, and a reported Al Qaeda (search) claim of responsibility in a videotape.

The tape raised the possibility that terrorists aligned with Osama bin Laden had changed the course of a national election. Spain's government has insisted its prime suspect in Thursday's rail bombings was the armed Basque separatist group ETA (search).

In one fell swoop, voters ousted Aznar, whose party was favored to win just days ago, even though he brought Spain eight straight years of economic growth, made it a founding member of the euro single currency, cut unemployment in half and brought a degree of prominence to a long-ignored country.

With 99 percent of the votes counted, the Socialists soared from 125 seats to 164 in the outgoing 350-seat legislature. The ruling Popular Party fell from 183 to 148. It cannot try to form a coalition because it has no virtually no allies in the legislature, where it had enjoyed a majority and was often accused of riding roughshod over opponents.

Rodriguez Zapatero started his victory speech by remembering those killed in the railway bombings. "At this moment I think of the lives that were broken by terror on Thursday," he said, then asked the crowd to join him in a minute of silence.

"My most immediate priority will be to fight terrorism," he said.

The Spanish Socialist Workers Party ruled from 1982 to 1996 but ran afoul of corruption scandals and was voted out in 1996, when Aznar took power.

Savoring victory again, outside Socialist party headquarters 1,000 jubilant supporters cheered and waved the party's red flag. But they, too, mourned those killed in the railway blasts. "Not all of us are here. Two hundred are missing," the crowd shouted.

"I think the party won because of people's frustration people about the Popular Party getting us into the war in Iraq," said one of them, housewife Loli Carrasco Gomez, 36.

Of the troops in Iraq, she said: "I hope they all come home and never go back."

Ruling party candidate Mariano Rajoy, Aznar's hand-picked successor, called Zapatero to congratulate Zapatero.

Outside the Popular Party headquarters, some 100 supporters chanted "Viva Espana! Viva Aznar" and waved party flags although there was nothing to celebrate.

Aznar chose not to seek a third term, saying he wanted renewal in government and his party.

Pre-election polls had favored his ruling party to win handily.

But on election day voters expressed anger with the government, accusing it of provoking the Madrid attacks by supporting the U.S.-led war in Iraq, which a vast majority of Spaniards opposed.

The government had insisted that its prime suspect in Thursday's rail bombings was the armed Basque separatist group ETA, even as evidence mounted of an Islamic link in the bombings.

The government was accused of withholding information on the investigation to save the election.

Throughout Sunday, voters said they lost faith in the ruling party.

"I wasn't planning to vote, but I am here today because the Popular Party is responsible for murders here and in Iraq," said Ernesto Sanchez-Gey, 48, who voted in Barcelona.