Spanish Prime Minister Mariano Rajoy has been forced out of office by a no-confidence vote in parliament.
Socialist leader Pedro Sánchez, who filed the motion after Mr Rajoy's party was implicated in a corruption scandal, will become prime minister.
"We're going to sign a new page in the history of democracy in our country," Mr Sánchez said ahead of Friday's vote.
Mr Rajoy is the first prime minister in modern Spanish history to be defeated in a no-confidence motion.
The leader of the conservative Popular Party (PP) has been prime minister since 2011.
During the second day of debate on Friday, Mr Rajoy admitted facing defeat and told MPs that it has been "an honour to leave a better Spain than I found".
Mr Sánchez secured a majority in the vote after gaining support from various smaller parties, including the Basque Nationalist Party - 180 MPs backed the motion, 169 voted against, with one abstention.
Why was the vote called?
Mr Sánchez says Mr Rajoy, 63, had failed to take responsibility for his party's involvement in the scandal, which hit the headlines again last week after one of its former treasurers was given a 33-year jail sentence.
The High Court in Madrid convicted Luis Bárcenas of receiving bribes, money laundering and tax crimes.
The case centred on a secret campaign fund which the PP ran from 1999 until 2005.
Many Spanish voters, exasperated by corruption scandals involving the traditional centre-right PP and centre-left Socialist parties, have abandoned them for newcomers like the left-wing Podemos (We Can) and pro-market Ciudadanos (Citizens), as well as regional parties.
A 'Frankenstein coalition'
Analysis by Gavin Lee, BBC Europe reporter
Mariano Rajoy walked out of Congress and out of a job this afternoon. With a fixed smile, he hugged his staff who cheered and lined the way to the last ride in his parliamentary car.
Cheers turned to boos outside the parliament's gates, with hundreds of protesters screaming "Good riddance", and placards which read "PP - Corrupting Spain since 1990".
The party was explicitly linked to what the judge called "institutionalised corruption" and questioned the court testimony of the now-former PM.
Mr Rajoy, the seemingly bulletproof leader who had long resisted calls for his resignation and who survived a previous no-confidence vote, has gone.
His successor, Pedro Sánchez, nicknamed "Mr Handsome", now has several expectant parties with conflicting interests to see to, for agreeing to oust Mr Rajoy.
It's been dubbed the "Frankenstein coalition" because while many are optimistic for a new kind of politics without corruption, some critics say he may instead have created a political monster.
Who is the new prime minister?
Profile by Guy Hedgecoe, BBC News, Madrid
Pedro Sánchez emerged as a virtual unknown to win the Spanish Socialist party premiership in 2014. The photogenic economist and former basketball player won members over with a promise to unite a divided party and put the Socialists back in power.
Yet he subsequently suffered two humbling election defeats, in 2015 and 2016. He was eventually forced to resign after his refusal to back Mariano Rajoy in an investiture vote plunged the country into a prolonged political stalemate and his party into bitter infighting.
Months later he confounded his many critics by returning to win the Socialist primary. Spain's constitution states that the party presenting a no-confidence motion must be prepared to govern and replace the deposed prime minister if a parliamentary majority backs it.
Therefore this moderate but ambitious 46-year-old from Madrid is set to be Spain's new prime minister, despite the fact that his party commands less than a quarter of seats in Congress.
What happens now?
Mr Rajoy's departure casts the EU's fifth-largest economy into political uncertainty.
Mr Sánchez is expected to be sworn in at the weekend and to name a cabinet next week.
Although he leads the Socialist PSOE party, he is not a member of parliament. Correspondents say that with only 84 lower house seats, the party will struggle to find allies to get legislation enacted.
Mr Sánchez is expected to get support from Podemos. Smaller groups - including Basque and Catalan nationalists - supported the no-confidence motion, but it is unclear whether they will back the new government.
The Ciudadanos party, which had been doing well in opinion polls, supported Mr Rajoy.