The Labour leader headed to the West Yorkshire constituency to celebrate Kim Leadbeater's narrow 323-vote win.
He said it was a victory for "hope over division, and decency over hatred", after a bitter campaign, and "just the start" for Labour.
Ms Leadbeater will now represent the seat previously held by her sister Jo Cox, who was murdered there in 2016.
Sir Keir hailed her "incredible courage" in standing for the seat, and claimed "Labour is coming home".
Writing in the Guardian, he also promised the by-election would be "a turning point, a sign that the politics of division and dishonesty won't win out, that unity and decency can".
The result will come as a relief to Sir Keir, who has been under pressure following recent election defeats for his party.
Prime Minister Boris Johnson said the Conservative candidate Ryan Stephenson ran an "incredibly positive campaign" and "did very well to reduce a longstanding Labour majority".
Speaking at a Downing Street press conference with German Chancellor Angela Merkel, Mr Johnson claimed the by-election result was the "third biggest swing in favour of the government since the War".
Ms Leadbeater took the seat with 13,296 votes, giving her a slim victory over Mr Stephenson with 12,973.
Former Labour and Respect MP George Galloway, standing for the Workers Party of Britain party, came third with 8,264.
The former Bradford West MP had sought to win support from the seat's Labour voters, with the aim of toppling Sir Keir as Labour leader.
His campaign had targeted 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.
Speaking in Cleckheaton, Sir Keir said Ms Leadbeater had won despite a split in Labour's vote, because "former Tory voters voted for her".
"The Tories had a clean hit, nobody was going against them, and they didn't win," he added.
Writing in the Guardian on Saturday, he said: "And as we emerge from restrictions, there is now a real opportunity to harness the solidarity and national spirit that we've seen over the last year.
"To make sure that after the last year of sacrifice, we build a more united, fair, and secure country.
"To rebuild our economy and our public services, to bring our communities back together and to put honesty and integrity back at the heart of our politics."
Sir Keir was reportedly facing a potential challenge from his deputy Angela Rayner if the party lost another northern English constituency to the Tories.
It comes after the Tories took Hartlepool, a once rock-solid Labour seat in May, alongside losing more than 300 councillors in England's local elections.
But Andrew Scattergood, co-chair of the Momentum campaign set up to campaign for former Labour leader Jeremy Corbyn, said the party 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.
The Batley and Spen result will take some pressure off Sir Keir Starmer.
The atmosphere in Labour circles at Westminster since May's local elections has been febrile.
Some Labour MPs fear things can only get worse - that more seats that were once safe are at risk.
There was a swing of nearly 3% from Labour to the Conservatives in Batley and Spen - if a similar swing was reproduced nationally, at a general election, Labour would have lost 11 seats.
Labour know their party's problems go deeper than the question of leadership - but preparations were being made for a worse result in Batley and Spen.
Sixteen candidates in total ran in the 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.