Spain's ruling Popular Party has suffered an unexpected defeat in the country's general election.
The Madrid blasts are thought to have swayed the vote
The poll was overshadowed by claims that al-Qaeda carried out the Madrid bomb attacks that killed 200 people.
The Socialist party's Jose Luis Rodriguez Zapatero is now set to become Spain's new prime minister, ending eight years of conservative rule.
"My immediate priority will be to fight all forms of terrorism," he said in a victory speech on Sunday night.
Mr Zapatero was - until Thursday's bombings - considered an outsider for Spain's top job.
But the Socialists won 42% of the vote while the centre-right Popular Party garnered 38%, according to official results.
Provisional Spanish election results
Socialists (PSOE): 43%
Popular Party (PP): 38%
Catalan Regional Party (CiU): 3%
Republican Left of Catalonia (ERC): 2.5%
United Left (IU): 5%
However, as the Socialists did not win an absolute majority, there will be much political horse-trading to come.
Mariano Rajoy of the ruling Popular Party has congratulated the socialists on their victory.
"We will have the interests of Spain at heart," he said.
A larger than expected 77% of the electorate turned out to vote.
Analysts said people had turned out in bigger numbers than predicted in order to defy the bombers who carried out last Thursday's attacks.
But the BBC's Chris Morris in Madrid 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, he says.
Investigations are continuing into who was behind the bombings.
Initially, the government was adamant the Basque separatist organisation Eta was responsible, but now it has been forced to admit that al-Qaeda has become the top suspect.
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.
The videotape was found in a litter bin on Saturday following an anonymous tip-off to a Madrid television station.
In the video, a man speaking Arabic with a Moroccan accent says the attacks were revenge for Spain's "collaboration with the criminals Bush and his allies".
A BBC correspondent in Madrid says criticism of the way government ministers handled the initial investigation into the attacks may have lost them the election.
"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.
Spanish 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.
"I've come to show that everything carries on, that we cannot stand idle," he said, bandaged and wearing a neck brace.
Wounded Cayetano Abad voted to show "we cannot stand idle"
Many people admitted they had not planned to vote until the bombings.
"I have two friends who have never voted in their lives and they're going to vote in this one," said 41-year-old businessman Carlos Bermudez.
Outgoing Prime Minister Jose Maria Aznar and his wife were booed and jostled as they arrived to cast their votes.
As he tried to address supporters, he was drowned out by cries of "manipulators", "liars" and "peace".
The Spanish government backed the US-led invasion of Iraq last year despite polls showing 90% opposition to it from the Spanish public.