Socialist Party leader Jose Luis Rodriguez Zapatero has been approved by the Spanish parliament to be the country's next prime minister.
Mr Zapatero will meet the king before taking office
He won by an absolute majority in the first round vote on Friday with support from PSOE and small regional parties.
Mr Zapatero will be formally appointed at a meeting with King Juan Carlos and will assume his duties on Saturday.
In a speech in Thursday's debate in parliament, he vowed as prime minister to wage a relentless war on terrorism.
His PSOE party won elections held three days after the Madrid train bombings.
"The government ... will make its priority aim the relentless fight against terrorism," Mr Zapatero said, was setting out his plans for the next four years.
In Friday's vote Mr Zapatero secured 183 votes, with 148 votes against from the outgoing Popular Party and 19 abstentions from the Basque and Catalan nationalist parties.
The 11 March train attacks and outgoing Prime Minister Jose Maria Aznar's support for the war in Iraq are said to have swayed many voters towards the Socialists.
Mr Zapatero had long been part of the anti-war movement and had pledged to withdraw Spanish troops from Iraq if elected - a promise he reiterated on Thursday.
Mr Zapatero said Spanish troops must have a UN mandate
He also emphasised his support for the European Union, saying he would do everything he could to ensure a constitution was signed by the summer.
He set out reform plans for Spanish political life and in the education and employment sectors.
Mr Zapatero started his speech, which lasted just over an hour, by paying tribute to the victims of the Madrid attacks, which left 192 dead.
He condemned any sort of terrorism and praised the people of Madrid and the rest of Spain for their heroism in the face of the atrocities.
Eighteen people, 14 of them Moroccan, have now been provisionally charged over the bombings.
Mr Zapatero pledged to improve the co-ordination of the security forces, boosting liaison with authorities in the Basque region and Catalonia as well as international organisations.
Turning to Iraq, he said the situation in the country continued to deteriorate and Spain would continue to push for stability, democracy and reconstruction in Iraq "but not without or against the Iraqis".
He said he would not allow Spain's "strong commitment to international security and to the fight against terrorism to be called into question".
But he reiterated that Spanish troops could only remain in Iraq under a UN mandate.
He said PSOE's victory in the elections showed the Spanish people's wish for change, for "a better Spain, a Spain that we deserve".