Sir Keir Starmer declared that “Labour is back” after the UK’s main opposition party won the Batley and Spen by-election by a whisker, defying expectations and giving the beleaguered party leader a reprieve.

Starmer’s team had been braced for an outbreak of civil war if it had lost the seat in West Yorkshire — after a string of dismal electoral results in the spring.

Instead, the Labour leadership has silenced its internal critics — for now — by winning the seat by a margin of just 323 votes, albeit down from more than 3,000 at the 2019 general election.

Kim Leadbeater won 13,296 votes, narrowly defeating the Conservative candidate Ryan Stephenson, the bookmakers’ favourite, who secured 12,973 votes.

George Galloway, the controversial former Labour MP who stood as a Workers Party of Britain candidate, came third with 8,264 votes. Turnout was 48 per cent.

The victory in West Yorkshire buys time for Starmer, who said it was a “fantastic result” for Leadbeater after she ran a “positive campaign of hope, in the face of division”.

“We won this election against the odds, and we did so by showing that when we are true to our values — decency, honesty, committed to improving lives — then Labour can win,” he added.

Instead of facing leadership challenges from the left of the party, Starmer now has the rest of the summer to inject fresh life into his political project ahead of the critical Labour party conference in late September.

“I think the next three months is critical,” said one party aide. “It’s a brilliant win and it has re-energised everyone. The left won’t try anything now.”

One member of the shadow cabinet said the mood was “very, very positive” after the “massive boost” for Starmer: “Labour are back in business.”

Labour had been on the back foot after the party lost the Hartlepool by-election and shed more than 300 councillors in the local elections in May. It also recorded its worst-ever by-election result in the Chesham and Amersham poll two weeks ago.

Colleagues noted a cheerful video clip of Starmer surrounded by clapping supporters on Friday, in contrast to a glum video of the leader alone in his office in the aftermath of the Hartlepool defeat.

With Labour far behind the Tories in the national polls, a leadership challenge had looked likely. Diane Abbott, a close ally of former leader Jeremy Corbyn, had predicted: “If Labour loses again, it must surely be curtains for him [Starmer].”

Starmer’s team had been nervous about a potential challenge by Angela Rayner, the deputy leader, whom he had unsuccessfully tried to demote after the Hartlepool result.

Leadbeater is the sister of Jo Cox, the constituency’s former MP who was murdered by a white supremacist just days before the EU referendum in 2016.

Though Labour has held the seat since 1997, it was in Conservative hands throughout the 1980s and 1990s.

Boris Johnson, the prime minister, who campaigned in the constituency along with many cabinet ministers, said: “I thought in Batley and Spen we fought an incredibly positive campaign and all things considered we did very well.”

Amanda Milling, Conservative party co-chair, said: “We always knew this was going to be a tough fight, but Ryan [Stephenson] worked hard to fight for change. This was always Labour’s to lose and they have only just managed to hold.”

The by-election was called after Tracy Brabin, the previous MP, resigned to become the first elected mayor of West Yorkshire.

Campaigners have complained about a by-election marred by abuse and allegations of dirty tricks. Video footage last week showed Leadbeater being harassed by supporters of Galloway, who had heavily targeted the local Muslim community with a pro-Palestinian message.

Labour’s national campaign co-ordinator Shabana Mahmood said on Sky News that Galloway’s campaign “spread a politics of fear” and was “divisive”.

Leadbeater defended her small majority, arguing that “it was always going to be close” but said that she was “pleased” with the outcome. “If I can be half the MP that Jo was I can be very, very happy and very, very proud of that”.

Galloway said he would seek to challenge the result in the courts after what he suggested had been false claims by political opponents.

Other factors that may have affected the vote include the Matt Hancock scandal. The former health secretary resigned last weekend after pictures emerged of him kissing a female aide in his office in breach of the lockdown rules he had imposed.

Milling admitted that the Hancock affair “was something that came up on the doorstep”. “I have to be honest about that”, she told Sky News on Friday.