Cara Hilton won the seat for Labour in a poll was triggered by the resignation of shamed politician Bill Walker, who was jailed last month for abusing his three ex-wives.
Ms Hilton secured the seat by nearly 3000 votes.
She said afterwards: "After the disgrace of Bill Walker, Dunfermline deserves better and I will ensure that we will be better - far better than what went before."
The SNP had taken the seat in the 2011 Scottish election, finishing with 590 more votes than Labour.
But Walker's actions meant the party was forced to defend the west Fife constituency mid-way through its second term in government.
The by-election, with a turnout of 42.75%, was declared shortly before 2am.
Ms Hilton, who was elected with 10,275 votes, said Labour was reconnecting with voters. And she claimed independence had been a turn-off among the electorate.
"The people of Dunfermline have rejected Scotland being put on pause for another year," she said.
"The government of Scotland has been suspended so that a referendum campaign can be won.
"We need a Scottish Government that will address the needs of Scots, not one that will simply make promises about what will happen after 2016.
"Today Dunfermline has sent a message to Bute House and Alex Salmond: it's time for you to focus on the real priorities of Scots, not your constitutional obsession.
"Use the powers you have now to make a difference, not just argue for more in the future."
She also praised the action of Walker's ex-wives, which triggered the electoral contest.
"We are only here tonight because of the bravery of three women," she said.
"Three women who came forward to demand justice and who won that fight.
"Their courage and determination must be a reminder to all of us that we need to work harder to ensure that all victims of domestic violence are able to come forward and receive the justice they deserve."
Although Walker had won the seat for the SNP in 2011, he resigned as an Independent MSP, having been kicked out of the SNP when the abuse allegations surfaced.
SNP candidate Shirley-Anne Somerville, who attracted 7,402 votes, said her party could remain proud of irs campaign, which focused on the threat of school closures in the area.
"This has been a long and interesting campaign for all of us," she said. "It is one that the Scottish National Party can be particularly proud of.
"We've run a positive campaign trying to support local parents in their schools and I hope we can come together, all of us in the party, to make sure those three schools in the Dunfermline constituency stay open."