Texas Senator Ted Cruz has won the Iowa Republican caucuses, in the first vote of the US presidential campaign.
"Tonight is a victory for courageous conservatives," he declared, to great applause, as he railed against Washington, lobbyists and the media.
He took 26% of the Republican vote, beating his rival, the frontrunner Donald Trump, and Marco Rubio.
Votes in the Democratic race are still being counted, with Hillary Clinton's camp saying they have narrowly won.
The aim of the primary and caucus races in the coming months is to determine which candidates will stand for the two main parties in the November presidential election.
Iowa caucus results
Republican vote, 99% reported:
- Ted Cruz: 26%, eight delegates
- Donald Trump: 23%, seven delegates
- Marco Rubio: 23%, seven delegates
- Ben Carson: 10%, three delegates
- Rand Paul, Jeb Bush: one delegate each.
- Chris Christie, Carly Fiorina, Jim Gilmore, Mike Huckabee, John Kasich and Rick Santorum: no delegates
Democratic vote, 99% reported:
- Hillary Clinton: 50%, 22 delegates
- Bernie Sanders: 47%, 21 delegates
- Martin O'Malley: 0%, no delegates
source: The Associated Press
Mrs Clinton's campaign director in Iowa, Matt Paul, said there was "no uncertainty" that the former secretary of state and first lady had beaten Bernie Sanders, a left-wing senator from Vermont.
In six precincts the vote was decided by the toss of a coin - all going to Mrs Clinton, according to the Des Moines Register.
Iowa Democratic race
Voters by age
17-29 year olds voted for Bernie Sanders
for Hillary Clinton
26% 65 and older voted for Bernie Sanders
69% for Hillary Clinton
Iowa's Democratic Party said with one precinct still to be called, their results were "the closest in Iowa Democratic caucus history".
Mr Sanders said it was a "virtual tie". Before leaving for New Hampshire, which is holding party primaries on Tuesday, Mrs Clinton told her supporters she was "breathing a sigh of relief".
There was no such ambiguity from Republican victor Mr Cruz, the 45-year-old conservative who is disliked by the Republican Party leadership.
"Iowa has sent notice that the Republican nominee and the next president of the United States will not be chosen by the media, will not be chosen by the Washington establishment," he said.
Analysis: Anthony Zurcher, BBC News, Iowa
In the end it was a victory for organisation over enthusiasm. Despite trailing Donald Trump in the polls for much of the last two weeks, Ted Cruz swept to a comfortable win in Iowa.
During his victory speech, he repeatedly thanked his grass-roots support - and for good reason. He and his campaign had invested considerable time and money to grind out a victory in this key state, and they were ultimately rewarded for their efforts.
With this result Mr Cruz now has the momentum to survive what looks to be an uphill battle among the more moderate voters in New Hampshire, and then win over the deeply conservative, evangelical voters of Southern states that dominate the primary calendar in the following weeks.
It appears increasingly likely that a showdown for Mr Cruz looms on the horizon with the surprise third-place finisher in Iowa, Senator Marco Rubio. And Mr Trump - even if his supporters did not turn out in the numbers expected - will surely remain a factor.
Mr Trump congratulated the Texas senator and said he was "honoured" by the second-place finish.
Mr Rubio, who has struggled to gain support in recent months, has performed far better than expected, and finished just one percentage point behind Mr Trump.
Meanwhile, two candidates are bowing out.
Sources close to Democrat Martin O'Malley, former Maryland governor, have told the BBC that he will suspend his campaign - narrowing the field to two competitive candidates.
On the Republican side, Former Arkansas Governor Mike Huckabee tweeted that he too would suspend his campaign.
US editorials on Iowa vote:
"Cruz's strategy, which included at least one visit to each of Iowa's 99 counties, paid off. He slogged his way across Iowa by car and by bus, stopping in small-town cafes and gymnasiums, while his biggest threat, Donald Trump, travelled by personal jet and by helicopter to large-scale rallies.
"In the short term, Donald Trump was the biggest loser - true of any front-runner but even truer of a candidate whose campaign raison d'etre is that he is a winner."
"His highly enthusiastic crowd cheered his promises to "break up the banks", regardless of whether that is remotely possible."
"If the past eight months have taught anything, it is that the political landscape can change dramatically, defying all predictions. This race is just beginning, and candidates who live by the polls can die by them as well."
"Most candidates this year spent more time listening than talking. This makes for better candidates, and better presidents."
Iowa has an unusual election system based on caucuses, which involve people gathering at private homes, schools and other public buildings across the state.
Democratic voters divide themselves into groups based on their preferred candidate, but the Republican caucus process is more like a traditional ballot.
More on Senator Ted Cruz
- How does a US election work? If you want to be president, it helps to be governor, senator, or five-star military general - and have lots of patience