Spain's Socialist Party has won a shock election victory after voters appeared to turn on the government over its handling of the Madrid bombings.
The Madrid blasts are thought to have swayed the vote
Jose Maria Aznar's Popular Party insisted Basque militants had carried out the attacks that killed 200, despite possible links to al-Qaeda.
Jose Luis Rodriguez Zapatero, Spain's new prime minister-elect, has said his first priority is to fight terrorism.
Socialists won 42% of the vote, while the centre-right Popular Party won 38%.
The BBC's Chris Morris, in Madrid, says the train bombings did more than shock Spain to the core; they proved to be the decisive factor in the general election that ousted the government.
Mr Zapatero was - until Thursday's bombings - considered an outsider for Spain's top job.
Despite his party's victory, however, there is expected to be much political "horse-trading" because the Socialists did not win an absolute majority.
PROVISIONAL SPANISH ELECTION RESULTS
Socialists (PSOE): 42%
Popular Party (PP): 38%
Catalan Regional Party (CiU): 3%
Republican Left of Catalonia (ERC): 2.5%
United Left (IU): 5%
While Mr Zapatero said his first priority was to tackle terrorism "in all its forms", he is thought likely to do it in a very different way than the outgoing government.
He strongly opposed Spain's support for the US-led war in Iraq and our correspondent says he may now seek to withdraw Spanish troops who are serving in Iraq.
A larger than expected 77% of the electorate turned out to vote in the wake of last Thursday's attacks.
But our correspondent says the late swing to the Socialists also raises one disturbing thought - if al-Qaeda was responsible for Thursday's attacks, it appears to have had significant influence in changing the government of a leading Western democracy.
Investigations are continuing into who was behind the bombings.
A videotaped claim of responsibility by a man identifying himself as al-Qaeda's military spokesman in Europe forced the government to change its stance on the most likely suspects.
The tape - claiming revenge for Spain's "collaboration with the criminals Bush and his allies" - was found in a litter bin on Saturday following a tip-off to a TV station.
Outgoing Foreign Minister Ana Palacio told the BBC that Eta was still a strong suspect, and said police were not ruling out a possible collaboration between Eta and al-Qaeda.
Three Moroccans and two Indians are being held in connection with the attacks.
Germany has called an urgent meeting of EU interior ministers to discuss the situation.
Duty to vote
Black ribbons hung from polling booths and voters' lapels on Sunday.
Cayetano Abad, one of the 1,500 wounded in last Thursday's attacks, was driven to a polling station in an ambulance.
Wounded Cayetano Abad voted to show "we cannot stand idle"
Many people admitted they had not planned to vote until the bombings.
"It's the first time I voted. I feel very happy because the government had to change... because of the Iraq war," a Spanish law student told the BBC.
Outgoing Prime Minister Jose Maria Aznar and his wife were booed and jostled as they arrived to cast their votes.
The Spanish Government backed the US-led invasion of Iraq last year despite polls showing 90% opposition to it from the Spanish public.