WASHINGTON — Speaker John A. Boehner, an Ohio barkeeper’s son who rode a conservative wave to one of the highest positions in government, said Friday he would relinquish his gavel and resign from Congress, undone by the very Republicans who swept him into power.

Mr. Boehner, 65, made the announcement in an emotional meeting with his fellow Republicans on Friday morning as lawmakers struggled to avert a government shutdown next week, a possibility made less likely by his decision.

Mr. Boehner told almost no one of his decision before making it Friday morning. “So before I went to sleep last night, I told my wife, I said, ‘You know, I might just make an announcement tomorrow,’ ” Mr. Boehner said at a news conference in the Capitol. “This morning I woke up, said my prayers, as I always do, and thought, ‘This is the day I am going to do this.’ ”

His downfall again highlighted the sinewy power of a Republican Party faction whose anthem is often to oppose government action. It also made vivid the increasingly precarious nature of a job in which the will and proclivities of a politically divisive body must be managed. No House speaker since Thomas P. O’Neill Jr., who held the gavel from 1977 to 1986, has left the job willingly.

For Mr. Boehner, who has been pressured throughout his tenure to push for deeper spending cuts and more aggressive policy changes than were possible with President Obama in the White House, seemed both exhausted by the fight and yet at peace with his final move: to leave rather than face a potentially humiliating fight within his party.

“My first job as speaker is to protect the institution,” Mr. Boehner said. “It had become clear to me that this prolonged leadership turmoil would do irreparable harm to the institution.”

Looking poised and sounding rehearsed, Mr. Boehner became emotional as he recalled a moment alone on Thursday with Pope Francis when the pontiff asked the speaker to pray for him. Reflecting on that poignant scene and his unlikely ascent, Mr. Boehner said, “I never thought I’d be in Congress, let alone be speaker.”

Fond of saying “I’m a regular guy with a big job,” Mr. Boehner struggled almost from the moment he became speaker in 2011 to manage the challenges of divided government while holding together his fractious and increasingly conservative Republican members.

The tension has spilled over into the race for the Republican presidential nomination, in which several candidates have openly derided Republican leaders in Congress like Mr. Boehner. The loud and potent voices in the House largely reflect the steady shift of power in the Republican Party base from places like Mr. Boehner’s suburban Cincinnati district to areas that are largely Southern, rural and white.

Most recently, Mr. Boehner was trying to devise a solution to keep the government open through the rest of the year, but was under pressure from conservatives who told him that they would not vote for a bill that provided funding for Planned Parenthood.

Mr. Boehner’s announcement lessened the chance of a government shutdown because Republican leaders joined by Democrats will almost certainly go forward with a short-term funding measure to keep the government operating, and the speaker will no longer be deterred by those who threatened his job.

Mr. Boehner said he would leave at the end of October. The leading candidate to replace him is Representative Kevin McCarthy of California, the majority leader, who is viewed more favorably by the House’s more conservative members both for his willingness to bend to their will and for his cheerful manner.

Video

Speaker John A. Boehner, who announced that he would resign his House seat at the end of October, has presided over an era of great partisan battling.Published OnSept. 25, 2015CreditCreditZach Gibson/The New York Times

The preferred candidate among many Republicans, Representative Paul D. Ryan of Wisconsin, has said he does not want the job.

“John Boehner has been a great leader of the Republican Party and the House of Representatives,” Mr. Ryan said Friday in a statement.

“This was an act of pure selflessness.”

Whoever replaces Mr. Boehner will inherit the complicated dynamics that have bedeviled him. Republicans lack the 60 votes needed to cut off a filibuster in the Senate, and also the two-thirds majority required in both chambers to override a presidential veto.

“There are anywhere from two to four dozen members who don’t have an affirmative sense of governance,” said Representative Charlie Dent, Republican of Pennsylvania.

“They can’t get to yes. They just can’t get to yes, and so they undermine the ability of the speaker to lead. And not only do they undermine the ability of the speaker to lead, but they undermine the entire Republican conference and also help to weaken the institution of Congress itself.”

Even Mr. Boehner’s most strident opponents will almost certainly miss him for his ability to raise hundreds of thousands of dollars, for his critics as well as his allies.

Mr. Obama said Friday that Mr. Boehner’s resignation took him by surprise. Saying he called Mr. Boehner moments before holding a news conference with President Xi Jinping of China, he praised the speaker as a “good man” and a “patriot.” The president said that though they had often disagreed, Mr. Boehner had “always conducted himself with civility and courtesy with me.”

Mr. Obama promised to “reach out immediately” to the next speaker.

Mr. Boehner’s announcement came just a day after Pope Francis visited the Capitol, fulfilling a 20-year dream for Mr. Boehner, who hails from a large Catholic family, of having a pontiff address Congress.

Mr. Boehner wept openly as the pope addressed an audience gathered on the West Lawn of the Capitol on Thursday. He no doubt understood that it was his last grand ceremony as speaker and a capstone to a long political career that began in the Ohio Statehouse and led to a seat in Congress in 1990.

Representative Nancy Pelosi of California, the Democratic leader in the House, learned about Mr. Boehner’s resignation when she read a breaking news alert on a staff member’s phone.

“God knows what’s next over there,” she told staff members. Ms. Pelosi, who had been privately negotiating on a plan to keep the government open, told reporters that Mr. Boehner’s resignation was “a stark indication of the disarray of House Republicans.”

With antigovernment fervor helping to prompt Mr. Boehner’s decision, several candidates for the Republican presidential nomination were quick to try to capitalize on the animus.

Video

In 2010, The Times traveled to the hometown of the Republican House Minority Leader John Boehner to get a sense of how growing up near Cincinnati may have shaped him.Published OnOct. 14, 2010

At the Family Research Council’s Values Voter Summit, which was taking place a few blocks from the Capitol, many jumped to their feet and cheered when Senator Marco Rubio, Republican of Florida, announced that Mr. Boehner was resigning.

“It’s time to turn the page,” Mr. Rubio said, deviating from his prepared text in an assertion tailored to the audience, whose views align with many who wanted to oust Mr. Boehner.

Addressing reporters after his remarks at the conservative summit meeting, Senator Ted Cruz of Texas spoke harshly of Mr. Boehner.

“The early reports are discouraging,” Mr. Cruz said.

“If it is correct that the speaker, before he resigns, has cut a deal with Nancy Pelosi to fund the Obama administration for the rest of this year, to fund Obamacare, to fund executive amnesty, to fund Planned Parenthood, to fund implementation of this Iran deal, and then presumably to land a cushy K Street job after joining with the Democrats to implement all of President Obama’s priorities, that is not the behavior one would expect from a Republican speaker of the House.”

For decades, Mr. Boehner legislated as a stalwart Republican institutionalist. He became speaker after a Tea Party wave in the 2010 election swept Republicans into the majority in the House on a call to drastically curb federal spending and the role of government.

It was an agenda Mr. Boehner supported, but he quickly found himself challenged by the new members of Congress who questioned his commitment.

That conflict resulted in a 16-day government shutdown in October 2013, the brink of default on the nation’s debt and the undoing of former Representative Eric Cantor of Virginia, who was the House majority leader and was toppled in a primary by a Tea Party-backed challenger.

“Americans deserve a Congress that fights for opportunity for all and favoritism to none,” said Michael A. Needham, the chief executive of Heritage Action, a policy arm of the conservative Heritage Foundation. “Too often, Speaker Boehner has stood in the way.”