The result means Kim Leadbeater will represent the seat previously held by her murdered sister Jo Cox.
Labour’s Kim Leadbeater has narrowly won the Batley and Spen by-election following a bitter campaign.
After defeating her Conservative rival by just 323 votes, she said constituents had “rejected division” and “voted for hope”.
It means she will now represent the seat previously held by her sister Jo Cox, who was murdered there in 2016.
The result will ease pressure on Labour leader Keir Starmer after recent election defeats for his party.
Ms Leadbeater said the “vast majority” of her conversations with voters during the campaign were about local rather than national issues.
Asked on BBC Breakfast whether Sir Keir had been an asset or a problem, she said: “The focus of the campaign was very much listening to local people.”
The campaign in West Yorkshire was marred by acrimony, personal abuse and allegations of dirty tricks.
Jo Cox’s widower Brendan Cox said Ms Leadbeater had been “incredibly brave” to put herself forward, and he was “incredibly proud” of her.
“It was a very bruising and pretty horrible campaign at times,” he added.
Conservative candidate Ryan Stephenson came second with 12,973, with former Labour and Respect MP George Galloway third with 8,264.
Speaking outside the count, Mr Galloway said he would apply to have the result set aside by the courts, citing a “false statement” made about him during the campaign.
Mr Galloway, standing for the Workers Party of Britain party, had targeted Labour voters with the aim of toppling Sir Keir as Labour leader.
His campaign sought to win support from voters unhappy at the party’s stance on issues including the conflict between Israel and the Palestinians, and the disputed territory of Kashmir, claimed by both India and Pakistan.
An ethnically diverse seat, about 20% of the population is from an Asian background, with just under 19% being Muslim.
Sir Keir, who welcomed it as a “fantastic result”, will head to the seat on Friday to congratulate his party’s victorious candidate.
It had been reported he may have faced a challenge from his deputy Angela Rayner if the party lost another northern English constituency to the Tories.
It comes after the Conservatives took Hartlepool, a once rock-solid Labour seat in May, alongside losing more than 300 councillors in England’s local elections.
Andrew Scattergood, co-chair of the Momentum campaign set up to campaign for former leader Jeremy Corbyn, said Labour could not “portray this as a massive victory”.
“We squeaked it over the line and there are some serious concerns,” he told the BBC News Channel.
Labour has held the seat of Batley and Spen since 1997, so a victory might well sound like it was expected.
However, there was an expectation Labour would lose and there were a number of factors working against them.
One was the fact that the Labour party has struggled in by-elections recently and lost Hartlepool, for example.
There was the fact that George Galloway stood here and he took 8,000 votes from traditional Labour voters in the Asian community, which is fairly sizeable in Batley and Spen.
The expectation in Westminster was that the Tories would sneak through the middle and win this seat – that hasn’t happened.
Sir Keir Starmer will breathe a big sigh of relief, because if the Labour leader had seen another by-election loss today then he would have been in real trouble.
There would have been a lot of talk about leadership challenges, about the future of the party and about whether he had any prospect of really winning power at any stage if he was losing these by-elections halfway through a Conservative term.
The fact that hasn’t happened means his supporters are in a pretty good mood this morning and they’re saying this shows that some of the questions being asked of him over the last few weeks have been a bit over-hyped and that he can win.
Sixteen candidates in total ran in contest. Turnout was 47.6%, with 37,786 verified ballots cast out of a total electorate of 79,373.
The vote was triggered after the previous Labour MP for the seat, Tracy Brabin, became West Yorkshire’s first elected mayor in May.
She first won the seat in a by-election after Ms Cox was murdered by a far-right extremist in Birstall, part of the constituency, in 2016.
The Conservatives fought a relatively low-key campaign, with Mr Stephenson saying he was focused on talking to voters about “the issues that matter here”.
Conservative Party co-chair Amanda Milling said it had been a “disappointing result” for her party.
She said the recent resignation of Matt Hancock as health secretary after breaking social distancing rules was “one of a number of different issues that came up” that had been raised by voters on the doorstep.
But she added that for her party to “run this close” was still “exceptional”, given it has been in government since 2010.