"I am still fired up and ready to go," a defeated Obama told cheering supporters, repeating the line that forms a part of virtually every campaign appearance.
McCain's triumph scrambled the Republican race as well.
"We showed this country what a real comeback looks like," the Arizona senator told The Associated Press in an interview as he savored his triumph. "We're going to move on to Michigan and South Carolina and win the nomination."
Later, he told cheering supporters that together, "we have taken a step, but only a first step toward repairing the broken politics of the past and restoring the trust of the American people in their government."
McCain rode a wave of support from independent voters to defeat former Gov. Mitt Romney of Massachusetts, a showing that reprised the senator's victory in the traditional first-in-the-nation primary in 2000.
It was a bitter blow for Romney, who spent millions of dollars of his own money in hopes of winning the kickoff Iowa caucuses and the first primary - and finished second in both. Even so, the businessman-turned politician said he would meet McCain next week in the Michigan primary, and he cast himself as just what the country needed to fix Washington. "I don't care who gets the credit, Republican or Democrat. I've got no scores to settle," he told supporters.
After Iowa, Clinton and her aides seemed resigned to a second straight setback. But polling place interviews showed that female voters - who deserted her last week - were solidly in her New Hampshire column.
She also was winning handily among registered Democrats. Obama led her by an even larger margin among independents, but he suffered from a falloff in turnout among young voters compared with Iowa.
Word of Clinton's triumph set off a raucous celebration among supporters at a hotel in Nashua - gathered there to celebrate a first-in-the-nation primary every bit as surprising as the one 16 years ago that allowed a young Bill Clinton to proclaim himself "the comeback kid."
She had 39 percent of the vote in the Democratic primary to 37 percent for Obama, who is seeking to become the nation's first black president. Former Sen. John Edwards of North Carolina trailed with 17 percent. New Mexico Gov. Bill Richardson was fourth, polling less than 5 percent of the vote.
Despite running a distant third to his better-funded rivals, Edwards had no plans to step aside. He pointed toward the South Carolina primary on Jan. 26, hoping to prevail in the state where he was born - and where he claimed his only victory in the presidential primaries four years ago.
Among Republicans, former Arkansas Gov. Mike Huckabee, who won the leadoff Iowa GOP caucuses last week, was running third in New Hampshire.
McCain was winning 37 percent of the Republican vote, Romney had 32 and Huckabee 11. Former New York Mayor Rudy Giuliani had 9 percent, Texas Rep. Ron Paul 8.
Clinton's triumph was unexpected - and unpredicted.
Obama drew huge crowds as he swept into the state after winning Iowa. Confident of victory, he stuck to his pledge to deliver "change we can believe in," while the former first lady was forced to retool her appeal to voters on the run. She lessened her emphasis on experience, and sought instead to raise questions about Obama's ability to bring about the change he promised.
The grind took a toll on both of them.
Obama suffered from a sore throat, while Clinton's voice quavered at one point when asked how she coped with the rigors of the campaign. That unexpected moment of emotion became the talk of the final 24 hours of a campaign that was unlike any other in history.
Clinton's performance came as a surprise even to her own inner circle.
In the hours leading up to the poll closing, her closest advisers had appeared to be bracing for a second defeat at the hands of Obama.
Officials said her aides were considering whether to effectively concede the next two contests - caucuses in Nevada on Jan. 19 and a South Carolina primary a week later - and instead try to regroup in time for a 22-state round of Democratic contests on Feb. 5.
These officials also said a campaign shake-up was in the works, with longtime Clinton confidante Maggie Williams poised to come aboard to help sharpen the former first lady's message. Other personnel additions are expected, according to these officials, who spoke on condition of anonymity while discussing strategy.
Obama, who won the leadoff Iowa caucuses last week, looked for an endorsement from the powerful Culinary Workers union in Nevada in the days ahead. South Carolina's Democratic electorate is heavily black and likely to go for the most viable black presidential candidate in history.
The Republican race turns next to Michigan, where McCain and Romney already are advertising on television, and where both men planned appearances on Wednesday. Huckabee also was expected to campaign in the state.
According to preliminary results of a survey of voters as they left their polling places, more independents cast ballots in the Democratic race than in the Republican contest. They accounted for four of every 10 Democratic votes and about a third of Republican ballots. The survey was conducted for The Associated Press and the television networks.
Republicans were split roughly evenly in naming the nation's top issues: the economy, Iraq, illegal immigration and terrorism. Romney had a big lead among those naming immigration, while McCain led on the other issues.
Half of Republicans said illegal immigrants should be deported, and this group leaned toward Romney. Those saying illegal immigrants should be allowed to apply for citizenship leaned toward McCain, while the two candidates split those saying those here illegally should be allowed to stay as temporary workers.
Among Democrats, about one-third each named the economy and Iraq as the top issues facing the country, followed by health care. Voters naming the economy were split about evenly between Obama and Clinton, while Obama had an advantage among those naming the other two issues. Clinton has made health care a signature issue for years.
About one-third said if Bill Clinton were running, they would have voted for him on Tuesday.
It was hard to tell who needed a Republican victory more - McCain or Romney. McCain was the long-ago front-runner who survived a near-death political experience when his fundraising dried up and his support collapsed. He shed much of his staff and regrouped. An unflinching supporter of the Iraq war, he benefited when U.S. casualties declined in the wake of a controversial building in U.S. troops. By the final days of the New Hampshire race, he held a celebration of sorts to mark his 100th town hall meeting in the state he won eight years ago.
"It has all the earmarks of a landslide with the Dixville Notch vote," an upbeat McCain quipped - he got four votes there to Romney's two and one for Giuliani - as his campaign bus headed to a polling place in Nashua. The crowd of supporters was so big, that voters complained and a poll worker pleaded with McCain to leave.
Seconds later, the bus pulled away.
David Espo reported from Washington. AP writers Liz Sidoti, Nedra Pickler, Scott Lindlaw, Glen Johnson, Beverley Wang, Charles Babington, Holly Ramer and Clarke Canfield contributed to this report.