STOCKHOLM — After a deadlock lasting more than four months, Sweden got a new government on Friday, with the Social Democrats and Prime Minister Stefan Lofven holding onto power under an agreement that excludes a right-wing, anti-immigrant party.

The pact is a precarious one, creating a minority government run by a coalition that had to make concessions to some of its more conservative opponents. Both of the major, multiparty political alliances fell well short of an outright majority in the Parliament in the September elections, and forming a government 133 days later required splitting both blocs.

The Social Democrats and Green Party will govern without the third member of their campaign bloc, the Left Party. To consent to the government’s formation, the Center Party and the Liberals broke with the other two members of the center-right alliance they had joined in the campaign.

In Sweden, a government does not require majority support in Parliament. But a majority vote can prevent its formation, and an attempt to do that on Friday fell short, with 153 of 349 lawmakers voting to block the new government from taking office.