BRUSSELS — Britain and the European Union agreed on a Brexit deal Thursday, setting the stage for a fateful showdown in the British Parliament, where Prime Minister Boris Johnson faces an uphill struggle to marshal enough votes for his plan after three years of anguished, politically corrosive debate.

However, Mr. Johnson, who has yet to win a vote in Parliament as prime minister, may win even if he loses. He can rightfully say he negotiated an agreement but that Parliament is to blame. He is then likely to call for a general election in the coming weeks, hoping to win a mandate to do what Britain’s paralyzed political class has so far been unwilling to do: pull Britain out of the European Union as swiftly as possible.

Britain’s opposition Labour Party is determined to reject the agreement and defeat Mr. Johnson at the polls, and others are hoping to force a second referendum that could reverse Brexit altogether.

It all suggested a recipe for further convulsions in the messy divorce instigated by Britain that began with the first referendum in June 2016, in which voters chose to leave the European Union, polarizing British politics and testing the resolve of the bloc’s 27 other members to stay unified.