"Today is a new day in a new Socialist Party," declared a jubilant Mr Jose Luis Rodriguez Zapatero.
He was addressing the party congress after a marathon three-day meeting in which he had been elected secretary general by a majority of nine votes over his nearest rival, Mr Jose Bono.
He later promoted younger faces to seats on the executive committee.
The 35th party congress was an attempt to heal the wounds in the ranks of Spain's oldest political party after allegations of corruption and illegal activities during its 14 years in government and from losing two general elections in succession.
The party has been led by an interim committee, led by the Andalusian president, Mr Manuel Chaves, since its secretary general, Mr Joaquin Almunia, resigned immediately after its election defeat in March. The past four months have seen the infighting becoming increasingly bitter with four separate candidates jostling to replace him.
Mr Chaves was rewarded for his hard work with the presidency of the party, a position which has been vacant since the death a few months ago of the veteran socialist Ramon Rubiales.
Many delegates wanted the former prime minister and for 25 years Socialist secretary general, Mr Felipe Gonzalez, as president. But the majority preferred a complete clean-out of all the old guard and Mr Chaves is highly respected.
Until he announced his candidacy Mr Zapatero was a virtual unknown outside his local region of Leon. At 39 he was the youngest candidate and promised a "new way" and a renovation of the party.
In a closely fought battle with sessions lasting well into the dawn hours, Mr Zapatero narrowly defeated Mr Bono (50), president of Castilla La Mancha and a member of the "older" generation. The two other candidates, both women, came in way behind.
PSOE, the Spanish Socialist Workers' Party, underwent its last major renovation at Suresnes in France in 1972 when the party, then illegal under the dictatorship of General Franco, removed the old guard and replaced them with a new generation, most of whom had not even been born at the time of the Spanish Civil War.
Mr Zapatero's victory was in part due to the last minute support from the left-wing faction of the party and followers of Mr Alfonso Guerra, one of the Suresnes generation.
Many of his supporters switched their vote to Mr Zapatero rather than allow Mr Bono to be first past the post. They hoped to be rewarded with a newly invented vice secretary general position for Mr Juan Carlos Rodriguez Ibarra, but failed. An angry Mr Guerra damned the new faces with his own faint praise. "They have a second rate profile, but we will try to collaborate with them."
Mr Zapatero warned that the path would be hard and asked for time and patience to be a real opposition.
"This is our new Suresnes, and we will work to make ourselves into a strong opposition," he said.