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
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 ahead of the New Hampshire primary showing her trailing Obama by double-digits.
However, the former first lady 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," she said.
Obama, the Illinois senator bidding to be the first black president, 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".
"I want to congratulate Senator Clinton on a hard-fought victory here in New Hampshire. She did an outstanding job," he said.
Terry McAuliffe, Clinton's adviser and former democratic national committee chairman, said: "This is a big, big win for us. It's now a one-on-one race. It's Hillary Clinton versus Barack Obama. We've got 25 more states to go."
Polls closed around the state on Tuesday after a day of frantic last-minute campaigning and heavy voter turnout, signalling that many independents had showed up to vote.
Campaign and state officials reported large crowds at some polling stations, aided by the unseasonably balmy weather, with predictions of a record turnout.
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.
Democrats vote next in Nevada on January 19 before their January 26 showdown in South Carolina.
Republican Rudy Giuliani, former New York mayor, has focused his efforts on the Florida primary, which votes on January 29, with hope that a strong showing there will propel him into the February 5 contests with momentum.