During his campaign for governor, Mr. Kobach, 52, presented himself as an effective policymaker who did not yield to opposition. He promised to crack down on undocumented immigrants, cut the size of state government and reduce taxes.

“Some people on the left may disagree with some of the things I say,” Mr. Kobach told a crowd at a debate last month. But, he said, “I’ll deliver and I’ll get it done.”

At his watch party in Topeka, the mood turned somber soon after the polls closed. Mr. Kobach circulated through the crowd early in the night, pausing to pray with a group of supporters. But the crowd dwindled as Ms. Kelly ran up large margins and news agencies started to call the race for her.

Shortly before 11 p.m. local time, Mr. Kobach told the crowd that he had called to congratulate Ms. Kelly. “It was a tough, tough race,” he said. “We battled close to the very end. But this one just wasn’t God’s will.”

Ms. Kelly was relentless in trying to tie Mr. Kobach to former Gov. Sam Brownback, an unpopular Republican who left Kansas in January after presiding over big tax cuts and revenue shortfalls. Mr. Brownback, who served seven years as governor, was elected along with Mr. Kobach in 2010, and the two men have long shared a commitment to cutting taxes and enacting socially conservative policies.

Mr. Brownback, now a United States ambassador, promised Kansans that his signature tax cuts would provide a “shot of adrenaline” to the economy. But that growth failed to materialize, and the state was forced to make deep cuts to government services. Last year, a bipartisan group of lawmakers overrode Mr. Brownback’s veto and reversed most of the tax policy.

Mr. Kobach, who tried to distance himself from that policy on the campaign trail, said Mr. Brownback’s mistake was reducing taxes without first making large cuts to the size of state government.