Republican Mike Huckabee, the former Arkansas governor who won in Iowa last week, came third with 11 per cent.
John Edwards, a Democratic presidential hopeful and former North Carolina senator, fell behind Clinton and Obama into third place with 11 per cent.
Obama, senator from Illinois, is hoping to be the first African American president.
New Hampshire's voters refused to follow the lead of the Iowa primary, which last week gave Obama, 46, and Huckabee, 52, the first big wins of the US presidential race.
Many had written off McCain, an Arizona senator, in the summer when he was low on cash and shedding campaign staff.
"I'm past the age when I can claim the noun 'kid' no matter what adjective precedes it. But tonight we sure showed them what a comeback looks like," he said to his supporters who repeatedly chanted, "Mac is back!"
Clinton, 60, who finished third in Iowa, faced opinion polls before the New Hampshire primary showing her trailing Obama by double digits.
However, she won the primary by three percentage points, with 39 per cent to Obama's 36 per cent.
"Over the last week, I listened to you and in the process I found my own voice," she told her supporters.
"Now together, let's give America the kind of comeback that New Hampshire has just given me."
The 'tears effect'
Christopher Hitchens, author and columnist for Vanity Fair
magazine, told Al Jazeera that Clinton's win in New Hampshire is considered a "comeback" of her own.
"A lot of people seemed to identify with her when she was nearly brought to tears, claiming politics is more than just a game," Hitchens said.
"But the undecided voters tend to lean towards Obama - Clinton is popular among registered Democrat voters."
Obama had hoped for a second primary win that would solidify his hold on the top spot in the race.
Instead, with the final third of votes to be counted, Obama conceded defeat, but said to his supporters he was "still fired up and ready to go".
While Obama lost to Clinton, he received a boost on Wednesday when Nevada's most influential union endorsed him before the state's voting on January 19.
The Culinary Workers Union, with 60,000 members working in hotels and casinos in Las Vegas, is a major political force in Nevada.
"We had a wonderful dilemma," said D Taylor, the union's secretary treasurer. "It's been a very difficult decision.
"We understand we are going against the Democratic power establishment ... we are used to being underdogs."
Unite Here, the group's parent organisation, with 460,000 food service, gaming and other workers nationwide, also expressed support for Obama,
Obama is expected to travel to Las Vegas to appear at a rally on Friday, his local office said.
Democrats vote next in Nevada on January 19 before their January 26 showdown in South Carolina.
New Hampshire's primary is the second high-profile battleground, following Iowa, in the state-by-state process of choosing Republican and Democratic candidates for November's election to succeed George Bush as president.
The election race now heads into an intense month of campaigning culminating in the Super Tuesday nominating contests on February 5, when some 22 states pick presidential candidates.
Republicans will contest their next primary in Michigan on Tuesday, where Romney faces a possible must-win and McCain and Huckabee will also contend.