U.S. Senator Ted Cruz soundly defeated billionaire Donald Trump in Iowa's Republican nominating contest on Monday, upending the party's presidential race and creating a three-way competition with establishment candidate Senator Marco Rubio.

On the Democratic side, officials said former Secretary of State Hillary Clinton had won by a razor-thin margin against U.S. Senator Bernie Sanders of Vermont, the closest in Iowa Democratic caucus history.

Cruz, a conservative lawmaker from Texas, won the first state Republican contest in the 2016 race with 28 percent of the vote compared with 24 percent for businessman Trump. Rubio, a U.S. senator from Florida, came in third with 23 percent, making a stronger-than-expected finish.

With Democrat Clinton prevailing by only four delegates, according to party figures, Sanders, a self-described democratic socialist, declared the result a "virtual tie."

RELATED: Boston's Laughing Liberally comics weigh in on Iowa Caucus

"Hillary Clinton has won the Iowa Caucus," Matt Paul, the Iowa state director for Clinton said in a statement released in the early hours of Tuesday morning.

Cruz's win and Rubio's strong showing could dent the momentum for Trump, whose candidacy has alarmed the Republican establishment and been marked by controversies ranging from his calls to ban Muslims temporarily from entering the United States to promising to build a wall on the U.S.-Mexican border.

"Tonight is a victory for courageous conservatives across Iowa and all across this great nation," Cruz, 45, said during a victory speech lasting more than 30 minutes.

An uncharacteristically humbled Trump, 69, congratulated Cruz and said he still expected to win the Republican nomination. Opinion polls show Trump leading nationally and in New Hampshire, which holds the next nominating contest.

"I'm just honored," Trump said.

Unusually large crowds poured into schools, churches and other venues for the caucuses, in which voters gather together to select a candidate.

RELATED: Does winning in Iowa really matter?

Cruz's well established get-out-the-vote effort helped overcome the enthusiasm from large crowds that have shown up for Trump's rallies. Trump skipped the last Republican debate before the caucus because of a dispute with host FOX News. A Trump adviser said his second-place finish was expected.

Iowa has held the first contest in the country since the early 1970s, giving it extra weight in the electoral process that can translate into momentum for winning candidates.

Rubio, 44, may benefit from that momentum as much as Cruz. The Florida lawmaker established himself as the mainstream alternative to the two front-running rivals.

“Rubio has staying power. He weathered $30 million in negative ads and late deciders broke his way due to his upbeat and optimistic close,” said Republican strategist Scott Reed.

Cruz was buoyed by evangelical support and thanked God for his win.

The results of the Democratic race put pressure on Clinton to siphon support away from Sanders, who has won over politically left-leaning voters with his promises to take on Wall Street and start fresh with health care reform.

RELATED: Trump, Cruz jockey for conservative vote in Iowa

Clinton, 68, said she was breathing a "big sigh of relief" after the results. She lost Iowa to then-Senator Barack Obama in the 2008 Democratic race and never recovered.

"It is rare that we have the opportunity we do now to have a real contest of ideas,” Clinton said with her husband, former President Bill Clinton, and daughter Chelsea joining her on stage.

Sanders, 74, declared himself overwhelmed. The lawmaker, who smiled broadly as he addressed supporters, is leading in New Hampshire, home to next week's second contest, but trails Clinton in other states such as South Carolina, which holds the third contest.

"Nine months ago, we came to this beautiful state, we had no political organization, we had no money, we had no name recognition, and we were taking on the most powerful political organization in the United States of America," Sanders said.

Former Maryland Governor Martin O'Malley, who had trouble gaining any traction in the Democratic race, suspended his campaign after coming in third in Iowa with 0.6 percent.