But so far most of the results had been from urban areas where Clinton was expected to do well whereas Obama enjoys most of his support in the rural areas where results are not yet in.
John Edwards was far behind in third place.
McCain, the Arizona senator and staunch Iraq war supporter who just months ago was being written off as a lost cause, beat Mitt Romney, the former governor of Massachusetts who has poured tens of millions of dollars of his personal wealth into the race.
McCain won 37 per cent of the votes while Romney got 32 per cent.
"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," McCain told supporters after his win.
Mike Huckabee, who won in Iowa
last week, came in third with 12 per cent.
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.
McCain's victory pushed him into the top tier of candidates as the race headed into an intense month of campaigning culminating in the Super Tuesday nominating contests on February 5, when some 22 states pick presidential candidates.