Here are reactions from seven presidential candidates on the evening of the 2016 Iowa caucuses. (Sarah Parnass/The Washington Post)

Sen. Ted Cruz of Texas scored a hard-fought upset win over businessman Donald Trump in the Iowa Republican caucuses Monday night, while former secretary of state Hillary Clinton and Sen. Bernie Sanders of Vermont were locked in a virtual tie on the Democratic side with most of the votes counted.

Cruz made good on his bet that a methodical campaign organization would eclipse Trump’s media dominance in the first test of Republican voters. With 99 percent of the precincts reporting, Cruz was beating his rival by more than 5,100 votes, with Sen. Marco Rubio of Florida a close third.

Cruz appeared to capitalize on deep support from religious and social conservatives and showed that old-fashioned retail politicking could overcome Trump’s massive political rallies in the Hawkeye State.

“God bless the great state of Iowa,” Cruz told supporters at his campaign’s Iowa headquarters after embracing his wife, Heidi. “Tonight is a victory for the grass roots. Tonight is a victory for courageous conservatives across Iowa and all across this great nation. 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, will not be chosen by the lobbyists.”

[Voting and analysis: 2016 Iowa election results]

Clinton had been a clear front-runner in Iowa last summer, as she hoped to make up for her loss to Barack Obama in the 2008 Democratic caucuses. But Sanders, hammering a message about economic inequities in the middle class, narrowed the margin in Iowa into a dead heat in the final days, and the results could presage a long, grueling fight to the nomination. He maintains a polling advantage in New Hampshire, which will hold its primaries next week.

With 95 percent of the precincts reporting, Clinton had 49.8 percent of the vote to Sanders’s 49.6 percent. Clinton, joined by her husband, former president Bill Clinton, and daughter Chelsea, addressed supporters before the final tally was in, saying she was “breathing a sigh of relief.”

“It is rare that we have the opportunity we have now to have a real contest of ideas, to really think hard about what the Democratic Party stands for,” Clinton said. “I am excited about really getting into the debate with Senator Sanders about the best way forward to fight for us and America.”

Sanders, accompanied by wife, Jane, appeared in front of a jubilant crowd that chanted “Feel the Bern!” He said that his showing against Clinton sent a message to the political and media establishments that “given the enormous crises facing our country, it is just too late for establishment politics and establishment economics.”

The night also began to winnow the field on both sides. Former Maryland governor Martin O’Malley, the only other major Democratic candidate, had negligible support and suspended his campaign Monday night, according to a person close to the campaign. Former Arkansas governor Mike Huckabee, a Republican, also suspended his campaign.

Iowa Caucus entrance poll results

The crowded Republican field and lively debates appeared to boost interest in the race. A record 185,000 people took part in the GOP caucuses, increasing the 2012 turnout by 5.4 percent, according to estimates from Edison Media Research.

Trump had led the Republican polls since last summer, shortly after he declared his candidacy, and he seemed to rewrite the traditional rules and expectations of national political campaigns. Yet his outsider appeal, which swelled crowds at rallies, failed to translate entirely to the caucus turnout, and some political analysts questioned his decision to drop out of the final GOP debate last week.

Appearing before supporters at his Iowa headquarters, Trump congratulated Cruz, but he reminded his audience of how far he had come.

“On June 16, when I started this journey, there were 17 candidates. I was told by everybody, ‘Do not go to Iowa. You could never finish even in the top 10,’ ” Trump said. “We finished second. . . .We will go on to get the Republican nomination, and we will go on to beat Hillary or Bernie or whoever they throw up there.”

Cruz, who had surged to a polling lead in Iowa by December but fell behind to Trump in the past few days, outperformed his final polling results in Iowa. Aides touted a strong ground game in which the senator appeared at events in all 99 counties. Cruz had 28 percent of the vote, with Trump at 24 percent and Rubio at 23.

Supporters at Cruz headquarters cheered loudly when their candidate was shown on a giant video screen. A cover band played songs of Patsy Cline and Johnny Cash as the audience exchanged hi-fives and hoisted drinks.

“That’s priceless. You’re fired!” Cruz precinct captain Ted Sturgill said, using Trump’s catchphrase from “The Apprentice” television show against him.

Whether Cruz can translate the win into similar success in New Hampshire is uncertain, as the past two Iowa winners, Pennsylvania’s Rick Santorum in 2012 and Huckabee in 2008, failed to win the nomination.

Rubio did far better than expected, with a late rally as he sought to consolidate support among establishment Republicans who have doubted Trump and Cruz. Former Florida governor Jeb Bush, who started the campaign as a prohibitive front-runner with a massive campaign war chest, finished way back in the pack. Bush wasn’t even in the state on Monday, campaigning instead in New Hampshire.

“They told me I needed to wait my turn, that I needed to wait in line,” Rubio told an enthusiastic crowd at his Iowa headquarters.

“This is your turn!” a supporter responded.

“Tonight here in Iowa, the people in this great state sent a very clear message that after seven years of Barack Obama, we are not waiting any longer to take our country back,” Rubio said.

Voters gathered at caucus sites in 99 counties around the state. The last-minute lobbying officially began at 7 p.m. Central time, with votes following. Political operatives predicted a high turnout — and there were reports that some sites were so packed that officials had trouble closing the doors, despite a significant snowstorm that was bearing down on the state. Forecasters said the storm would probably hit after the caucuses were closed.

“I pray we will win,” Cruz said during a stop at a Baptist church in Marion.

Trump and Sanders made their final pushes Monday to coax nontraditional voters to the caucuses, even as their chief rivals suggested that well-tested organizing tactics would give them the crucial margin of victory.

Television networks showed Trump, in a solid red tie, visiting a caucus site in West Des Moines with his wife, Melania, in a matching all-red ensemble, and taking a seat in the front row. One woman approached and posed with him for a selfie on her mobile phone.

“We are going to bring our country back,” Trump told the crowd, speaking into a microphone. Reflecting on a campaign that was dismissed early on by the political establishment in Washington, Trump said: “It’s really been a journey, an amazing journey. I’m a messenger. We’re going to run it the way it’s supposed to be run — as a great, great country.”

[Here’s how the voting in Iowa works]

There was little question that the lack of an incumbent, coupled with the unconventional style of several candidates, has sparked Iowans’ interest in the race this year. Jeff Kaufmann, who chairs the Iowa Republican Party, said his office has received five to six times as many calls compared with past years.

“The phone calls at the Republican Party of Iowa headquarters are absolutely unprecedented. I mean, we’re looking at 100 an hour, literally,” he said. “Now, obviously, not all of that is tied to Donald Trump. There’s also a lot of these calls that are going to a variety of candidates. But I think that’s a sign of the enthusiasm.”

Iowa Gov. Terry Branstad (R) echoed that assessment in an interview, saying that Trump has “turned out bigger crowds than we’ve ever seen before.”

“I know everybody is saying, ‘Are they just coming for curiosity?’ But I think they’re for real; they’re committed and will show up for caucuses,” Branstad said. “I’ve been pretty impressed with what they’ve done.

“Of course, we saw this phenomenon eight years ago with Obama,” he said, referring to Obama’s first White House bid. “It was beyond what anyone could have imagined, and I think Trump is a phenomenon, too.”

The Democratic field was much smaller but no less competitive. Clinton, whose comfortable lead in Iowa evaporated earlier this year, has scrambled to try to fend off Sanders, who has attracted massive crowds and an intense grass-roots following that put an Iowa upset within reach.

Sanders was encircled by volunteers and a crush of reporters as he arrived late Monday morning at his Iowa headquarters in a strip shopping center outside downtown Des Moines.

“We have come a long, long way in the last nine months,” the senator told his supporters. “You’ve got a tied ball game, that’s where we are.”

Sanders also pushed back against critics who have suggested his agenda is too radical. “Our platform, our agenda, is precisely what the American people want,” he said.

Eilperin and Nakamura reported from Washington. Sean Sullivan, Robert Costa, Katie Zezima, Philip Rucker and John Wagner in Des Moines, and Abby Phillip in Council Bluffs, Iowa, contributed to this report.