Spain's caretaker socialist Prime Minister Pedro Sánchez has narrowly won a confidence vote in parliament, enabling him to govern in coalition with far-left Podemos.
The dramatic open vote, by a simple majority of MPs, went 167 to 165 in his favour. Abstentions by Catalan and Basque MPs played a critical role.
He will now form a minority government.
It will be Spain's first coalition government since democracy was restored in 1978, after the Franco dictatorship.
Mr Sánchez plans to hold his inaugural cabinet meeting on Friday.
The fragmented 350-seat parliament makes the passage of legislation difficult, as his Socialist Party (PSOE) has 120 seats and Podemos 35 - short of a majority.
Sánchez left-wing plans
The vote - or abstention - of each MP in turn was read out in the chamber and broadcast live on TV. It was extremely close, and right up to the end the "no" votes were ahead.
Addressing MPs before the vote, Mr Sánchez said such a coalition was "the only option" for Spain, after five elections in recent years.
"Either a progressive coalition, or more deadlock for Spain," he warned them.
The new coalition plans to raise income taxes for those who earn more than €130,000 (£111,000; $145,000) annually.
They also plan to reverse some labour market reforms passed by the previous conservative government, which made it easier and less expensive to fire workers.
MP defies cancer to vote
On Sunday Mr Sánchez lost a first confidence vote, when he failed to get an absolute majority in parliament to form a coalition government.
The extraordinary nature of this vote is underlined by the case of Aina Vidal, a Podemos MP who is in severe pain with cancer and had to miss Sunday's vote. She turned up for Tuesday's crucial vote, despite her illness.
DIRECTO | Un ramo de flores, gritos de 'sí se puede' y aplausos. Así ha sido el gesto de los diputados con Aina Vidal, que ha acudido a votar pese a que está gravamente enferma de cáncer #investidura https://t.co/8MJQd4nbdg pic.twitter.com/GH9ex8tmqV— EL PAÍS (@el_pais) January 7, 2020
After the vote MPs presented Ms Vidal with a big bunch of flowers and applauded her, shouting "yes we can" - echoing Barack Obama's catch-phrase. "Podemos" in Spanish also means "we can".
Earlier, PSOE spokeswoman Adriana Lastra accused right-wing MPs of "bullying".
She said MP Tomás Guitarte of the small Teruel Existe party had suffered so much pressure that he concealed his whereabouts out of fear. She said he had received more than 8,000 emails urging him to vote "no" instead of "yes".
Spain's politics was unstable last year, with two inconclusive elections in April and November.
The PSOE won again in November, but were weakened, taking 120 seats - three fewer than in April. The new far-right Vox party surged to third place, deepening Spain's political fragmentation.
Vox leader Santiago Abascal accused the PSOE on Tuesday of covering up gang rapes by illegal immigrants. He was the only leader not to congratulate Mr Sánchez.
Before the vote, opposition centre-right Popular Party (PP) leader Pablo Casado accused Mr Sánchez of acting as an "extremist" - not a moderate - by allying himself with the far left and the Basque and Catalan nationalists, without explaining that to the Spanish people.
"You lied and this stigma accompanies the government at its birth," he said.
The PSOE struck a deal which secured the narrow win for Mr Sánchez: the 13 MPs from Catalonia's largest separatist party, the ERC, agreed to abstain, as did the five from a Basque party, Bildu.
The ERC decision came after Mr Sánchez agreed to open a formal dialogue on the future of Catalonia, if confirmed as prime minister, and to then submit the dialogue's conclusions to Catalan voters.
The Catalan separatists' drive for independence overshadows Spanish politics, with the conservative and far-right opposition parties bitterly opposed to it.
Mr Sánchez said he wanted to free Spanish politics of its "toxic atmosphere". He said dialogue was necessary to "overcome the territorial disputes, always in line with the constitution".
The PSOE opposes granting Catalans a legal independence referendum, while recognising that both Catalonia and the Basque Country are nations within Spain, and not just regions. The Catalans and Basques already have a large degree of autonomy.