Rep. Tim Huelskamp lost his congressional seat Tuesday in a brutal Republican primary battle that pitted unlikely foes against each other while attracting an unusually large amount of money for a rural Kansas district.

Roger Marshall, a local obstetrician, bested the three-term congressman with the help of powerful groups both inside and outside the state, including the U.S. Chamber of Commerce and the Ending Spending political action committee.

Huelskamp notched high-level endorsements from Sens. Ted Cruz and Rand Paul, as well as the support of Koch-backed Americans for Prosperity and the Club for Growth.

Ending Spending, founded by former TD Ameritrade CEO Joe Ricketts, is more frequently seen backing candidates linked to the GOP establishment. The PAC supported incumbent Kansas Sen. Pat Roberts, for example, against a Tea Party primary challenge from another insurgent doctor, Milton Wolf, in 2014.

But the competitive primary in Kansas' first district inverted many of the dynamics that have governed GOP congressional battles since 2010.

In this primary, Tea Party groups and figures lined up behind an incumbent Republican while establishment-linked political organizations backed a candidate they had packaged as a conservative outsider.

The race served as a test of whether establishment institutions could succeed against a staunch Tea Partier by using the same playbook that brought such candidates to Washington in the first place.

Marshall's candidacy took on a new intensity after the influential Kansas Farm Bureau endorsed him over Huelskamp in early July, an unprecedented move for the group.

"He had a lot of difficulty dealing with other groups. He had a lot of diff dealing with his colleagues," Warren Parker, a spokesman for the Kansas Farm Bureau, told the Washington Examiner. "He has just worked himself into a situation of being irrelevant."

Huelskamp took heavy fire from the farm bureau and other groups for his votes against a farm bill that would have provided staples like crop insurance to farmers in his district. He said he did so because the legislation spent far more on food stamps than agricultural programs.

The congressman's opponents hammered Huelskamp for losing his seat on the agriculture committee, a key position he held until 2012. House Speaker John Boehner stripped Huelskamp of his seat after the Kansas Republican voted against Rep. Paul Ryan's budget.

Parker said farmers and ranchers in the district were angered that Huelskamp's inability to reach a consensus on the House budget cost them valuable representation on the agriculture panel.

A member of the House Freedom Caucus, Huelskamp had gained a reputation for obstruction after joining a handful of other conservative members in blocking high-priority legislation, such as the debt ceiling increase in 2014. He was among the vocal group of lawmakers who forced Boehner out of the speakership and demanded House leaders open up the legislative process to rank-and-file members.

The House Freedom Fund, the political action committee associated with the Freedom Caucus, shifted roughly $20,000 into Huelskamp's coffers, a caucus source told the Examiner.

In addition, individual members—including Reps. Gary Palmer, Justin Amash, Jim Jordan, Jody Hice, Barry Loudermilk, Paul Gosar and others—cut separate checks to the embattled Kansan directly from their own campaigns.

Reps. Jim Bridenstine, Steve King and Jordan each traveled to Huelskamp's district after the Republican convention to drum up support for him, the source said.

But their efforts could not save Huelskamp from the wave of antipathy that swept his district and carried Marshall to victory Tuesday evening in a race that drew more than $2.5 million in outside expenditures.

The U.S. Chamber of Commerce, viewed by many as the emblem of the Republican establishment, spent $400,000 on ads against Huelskamp and for Marshall in the first district of Kansas.

In a twist on prevailing GOP logic, the Chamber attempted to paint Huelskamp as a career politician who has contributed to problems in Washington despite his record of standing up to the same congressional leaders supported heavily by the Chamber.

The Kansas Chamber of Commerce rushed to defend Huelskamp on Monday, the same day Rob Engstrom, the Chamber's national political director, flew to Kansas to endorse Marshall on the eve of the primary.

"Contrary to claims by his opposition, Rep. Huelskamp hasn't 'gone Washington' and is everything but a typical politician,'" said Bill Pickert, chairman of the Kansas Chamber.

Huelskamp became the fourth incumbent member to be tossed out of Congress this year by competitive primaries after Reps. Chaka Fattah, Renee Ellmers and Randy Forbes suffered defeats in their respective races.

His loss at the hands of the Republican establishment could serve as a shot across the bow for Tea Party members who plan to buck House leadership on big-ticket legislation.