LONDON — Britain’s ruling Conservative Party shed thousands of votes to the insurgent, populist Brexit Party, suffering another crushing election setback early Friday, just hours before Prime Minister Theresa May planned to stand aside as leader.
In a closely watched by-election that was narrowly won by the opposition Labour Party, Mrs. May’s Conservatives were pushed into third place, behind the Brexit Party, led by Nigel Farage.
The result follows the Conservative Party’s disastrous performance in elections for Europe’s parliament last month. Mrs. May’s Tories finished fifth, with just 9 percent of the vote, one the worst performances in their long history.
The defeat in the Peterborough constituency marks another low point for Mrs. May as she ends her leadership of a Conservative Party that has been deeply wounded by her failure to extricate Britain from the European Union, as she had promised to do by the end of March. The election took place on Thursday and the results were declared early Friday.
With her Brexit plan having been rejected three times by Parliament, Mrs. May has promised to resign on Friday as Conservative Party leader, though she will stay on as a caretaker prime minister until her successor is in place.
For Labour, punished in the European elections for its lack of clarity on a second Brexit referendum, the victory in Peterborough was a welcome relief. However, both main parties, but particularly the Tories, know they will face a dire threat from Mr. Farage as long as Brexit goes unresolved.
Labour’s successful candidate, Lisa Forbes, won the contest with 10,484 votes, ahead of the Brexit Party with 9,801 votes and the Conservatives with 7,243.
“The fact that the Brexit Party have been rejected here in Peterborough shows that the politics of division will not win,” Ms. Forbes said.
Yet Labour’s victory over Mr. Farage’s new party was a narrow one. Labour won with just 31 percent of the vote, as against 29 percent for the Brexit Party. In the general election of 2017 Labour had secured 48 percent of the vote in Peterborough.
More of a social movement than a conventional political party, the Brexit Party has no real policies, but preaches a blunt message that Britain should leave the European Union as soon as possible, without any agreement if necessary, whatever the economic costs of doing so.
Mr. Farage has prospered by exploiting the frustrations of those who voted for Brexit in a 2016 referendum, only to see Mrs. May forced into requesting two delays to Britain’s departure.
His relative success in Peterborough, which voted to leave the European Union in the 2016 referendum, is likely to reinforce the belief among the Tories that to avoid an electoral catastrophe, their next leader must be a hard-line supporter of Brexit — like the former foreign secretary Boris Johnson — and someone who can neutralize the threat from Mr. Farage.
The progress of the Brexit Party is also likely to unnerve Labour, which had held the Peterborough seat until its lawmaker, Fiona Onasanya, was forced out after she was sentenced to three months in jail for lying about a traffic offense.
Last month, the Brexit Party sent tremors through British politics by winning 29 seats in the European Parliament in a vote that went ahead only because Britain had failed to leave the European Union on schedule on March 29.
That contest was held under a voting system that helps smaller parties more than the “first past the post” practice in normal British elections.
In the 2016 referendum on Brexit, a majority in Peterborough voted to leave the European Union, making this fertile ground for the Brexit Party. Nonetheless, Britain’s winner-take-all system for parliamentary elections makes it very hard for small parties to win seats. In his former incarnation as leader of the U.K. Independence Party, Mr. Farage, who is a member of the European Parliament, tried and failed to win election to Westminster no fewer than seven times.
For Mrs. May’s Tories, there was little to cheer because, under normal circumstances, they would have expected to do well in Peterborough, as Labour and the Conservatives have competed closely there in recent elections.
In the general election in 2017, the Conservative candidate lost by just 607 votes and, given the circumstances of Ms. Onasanya’s departure, the by-election would normally have been an ideal opportunity for the Conservatives to recapture the seat.