Labour held the Heywood and Middleton seat by just 617 votes as Nigel Farage claimed Ukip was “ripping lumps” out of the party in its northern heartlands.
Ed Miliband's party had held the seat with a majority of almost 6,000 in 2010 but faced a recount before claiming victory in the by-election.
Liz McInnes held off the challenge of John Bickley, with 11,633 votes to the Ukip candidate's 11,016 - a swing of 17.65 per cent swing from Labour to Ukip.
Ukip had been campaigning strongly in the Lancashire constituency, in an effort to make inroads into Labour's northern heartland.
The poll was triggered by the death of Labour MP Jim Dobbin, who had held the seat since 1997.
Mr Farage told Sky News: “We are ripping lumps out of the old Labour vote in the north of England. The truth of what has happened in the North today is that if you are anywhere north of Birmingham, if you vote Conservative you get Labour.
In pictures: The rise of Ukip
In pictures: The rise of Ukip
1/8 1993: Alan Sked forms Ukip
History professor Alan Sked had been active in anti-EU politics for a while beore he founded Ukip in 1993. He resigned from the party after the 1997 election, concerned that it was attracting far-right members, and has been critical of Ukip since. Picture: Reuters
2/8 2005: Kilroy defects
Former TV presenter Robert Kilroy-Silk founded Veritas in 2005, after a failed bid to become leader, and took many of Ukip's elected members with him. But the party slowly lost its popularity and didn't put forward any candidates in the last election. Picture: REUTERS/Kieran Doherty REUTERS KD/RUS
3/8 2010: Farage becomes leader, again
Farage had led Ukip from 2006 until 2009, when he stood down to fight against the Speaker, John Bercow, for his Buckingham seat. He failed to win the election and returned to lead the party in November 2010. Picture: REUTERS/Kieran Doherty
4/8 2010: Ukip fights for election
Nigel Farage was injured in a plane crash on polling day in the 2010 general election, but his party increased its success in the votes. It fielded 572 candidates and took 3.1% of the vote, though failed to win any seats. REUTERS/Darren Staples
5/8 2013: Eastleigh gains
Ukip's candidate Diane James got the highest ever number of votes for any candidate from the party, but was beaten by the Liberal Democrats. The surge in support gave Ukip confidence ahead of local and European elections later in the year. Picture: Reuters
6/8 2013: Bloom kicked out
Godfrey Bloom, who served as an Ukip MEP from 2004 to 2014, had the whip withdrawn in 2013 after sexist comments and an attack on a journalist. He sat as an independent MEP until 2014, when he ended his term in office. Picture: REUTERS/Luke MacGregor
7/8 2014: European election success
Ukip got a higher proportion of the vote than any other party in 2014's European elections, adding 11 new MEPs and taking its total to 24. REUTERS/Luke MacGregor
8/8 2014: Carswell defects
Douglas Carswell defected from Ukip at the end of August, and was followed by Mark Reckless at the end of September, who resigned from the Tories amid rumours of many more defections to come. Picture: REUTERS/Toby Melville
“And the reason we haven't won up there, despite a fantastic campaign, is that too many people have stuck with the Conservatives, not recognising that Ukip is now the challenger to Labour in every urban seat in the north of England.”
Mr Bickley said: “Labour are in big trouble. This should have been a safe seat. They have thrown everything at it and they have only just scraped home.
“We would not have predicted four weeks ago to get so close.
“Most of the voters here in this constituency, particular the Labour voters, feel betrayed by the Labour Party. These are people who have been in the Labour Party all their lives and they look at the Labour Party and it no longer represents them.
“Another couple of days and I would have won this. We would have won a week from now on the back of Clacton.”
Ms McInnes said: “I am proud to have been selected as the new MP for Heywood and Middleton. Our vote held up, we had not anticipated the Tory vote collapsing quite so dramatically. But our core voters turned out. We didn't take anyone's votes for granted and we have run a very strong positive campaign.”
Asked if she expected Ukip to run have Labour so close, she said: “To be honest with you I have been through more or less every scenario. As I said before, we don't take anyone's vote for granted. We work very hard and I am just pleased our vote held up. We can't choose our opponents.
“I am proud and honoured to have been selected for this area. I am not disappointed that the majority has been reduced. I knew this was going to be a tough fight and we have run a really strong positive campaign and we have fought for every single vote.”