WASHINGTON—House Speaker Paul Ryan said Wednesday he would retire at the end of the term, sparking an intraparty battle to succeed him and rattling Republicans who expect a fierce struggle to maintain their majority.

Less than three years into his tenure as speaker, Mr. Ryan said he would step down in January. While the timing of his announcement was a surprise, lawmakers have been privately weighing the odds of the two front-runners, Majority Leader Kevin McCarthy (R., Calif.) and Majority Whip Steve Scalise (R., La.) to replace him.

Mr. Ryan’s planned departure could add to the headwinds already buffeting the Republicans as the midterm elections approach because it is likely to weaken his power to raise money for the party.

“This is a public acknowledgment that the House is really, really gone,” said Dan Eberhart, a GOP donor and chief executive of a Colorado-based drilling services company. “Ryan has unzipped a deluge of donors that are now going to be focused more than they were on the Senate.’’

Since 1994, the president’s party has lost an average of 23 seats in the House midterm elections. The GOP this year is further burdened by President Donald Trump’s low approval rating and the planned retirements by an unusually large number of House Republicans, although many are hoping a strong economy will provide a boost for their party.