On Sunday, in the first round of voting by lawmakers, Mr. Sánchez failed to gain the absolute majority that he needed. But on Tuesday he needed only to receive more votes in favor than against.

The conflict over Catalonia dominated the acrimonious parliamentary debate, particularly after a recent pledge by Mr. Sánchez that his next government would hold a fresh round of negotiations with the separatist politicians who have governed in the region since 2015.

His offer of talks provoked a blistering response from right-wing politicians, who accused Mr. Sánchez of caving in to the separatists to continue in office and of paving the way for the breakup of Spain.

Before the vote on Tuesday, Pablo Casado, the leader of the Popular Party, which is now the main opposition, repeated his earlier warning that Spain was set to have “the most radical” government in its history.

He told lawmakers that the next Socialist-led government would be at the mercy of the “blackmail” of Catalan separatists, while also having to comply with the far-left demands of Unidas Podemos.

Directly addressing Mr. Sánchez, Mr. Casado said, “Now the problem will be yours: If you meet the demands of your anti-system partners, you will break up Spain. If not, they will throw you out onto the street.”

Mr. Sánchez responded by pledging that his government would end “the toxic climate” that Mr. Casado and other right-wing lawmakers were fueling and would instead work to ease nationalist tensions within Spain.