A motion of no confidence has been passed in Spanish Prime Minister Mariano Rajoy following a corruption scandal.
It passed by 180 votes to 169 with one abstention.
He is the first Spanish prime minister to lose such a vote since Spain transitioned to democracy in 1977.
Deputies were asked individually in the lower house of parliament, the Congress of Deputies, whether or not they supported the motion on Friday morning.
This was done in an individual roll-call of deputies.
Spain's Prime Minister Mariano Rajoy, bottom right, waves as members of his party applaud him during the second day of a motion of no confidence session at the Spanish parliament in Madrid | Image: Francisco Seco/AP/Press Association Images
This means Pedro Sánchez, the leader of the opposition PSOE party, is the new prime minister.
Mr Sanchez (46) is expected to take office by Monday, after King Felipe VI swears him in.
He is also expected to appoint his cabinet next week.
Mr Sanchez has already committed to respecting a budget passed by Mr Rajoy.
He has also promised to start talks with the Catalans over their bid for independence, but said he will not give them another referendum.
His party has a narrow majority in the 350-seat lower house.
Pedro Sánchez smiles at the start of the second day of a motion of no confidence session at the Spanish parliament in Madrid | Image: Francisco Seco/AP/Press Association Images
Mr Rajoy told the Spanish parliament he was proud of his record.
"It has been an honour to leave Spain better than I found it. Thank you to all Spaniards and good luck," he said.
The vote of no-confidence came after 29 former members of his conservative Popular Party were convicted of corruption last week.
The Spanish National Court found bribes were offered to former officials in exchange for public contracts.
The party also received a fine as a beneficiary of the kickbacks-for-contracts scheme.