WASHINGTON — House Majority Leader Kevin McCarthy abruptly withdrew his promising bid to become speaker Thursday as the married congressman was accused of having an affair with another married House member in an anonymous posting on Wikipedia.

Minutes before Republicans were to cast votes to make him speaker-elect, McCarthy stood and told his colleagues that a fresh face was needed to unite the party.

“And I’m not that person,” McCarthy said, according to one person in the room.

Members sat in surprise. At least one female House member started crying, said Rep. Peter King (R-LI).

“This was the shock of shocks,” King told The Post.

On Thursday, the Wikipedia entries for both McCarthy (R-Calif.), who has two children, and Rep. Renee Ellmers (R-NC), who has a son, were changed to allege the two were having an affair. They were later taken down.

The Internet address from which the changes were made originated from the Department of Homeland Security in Springfield, Va., according to the Washington Free Beacon.

The changes linked to an article posted on the news website GotNews alleging the two have had a long-running affair. The site reported that outgoing Speaker John Boehner had told McCarthy months ago to end the fling.

McCarthy and Ellmers both denied the affair. Homeland Security said it was investigating the posts.

“DHS has immediately launched an investigation into this serious matter,” spokeswoman Marsha Catron told The Post. “If it is discovered that a DHS employee, using government property, is responsible for these alleged actions, immediate and appropriate disciplinary action will be taken.”

McCarthy will stay on as No. 2 in the House. Boehner will also remain until a successor is chosen.

Rep. Walter Jones (R-NC) had sent a letter Tuesday asking that any leadership candidate withdraw from the election “if there are any misdeeds he has committed since joining Congress that will embarrass himself.”

Flanked by his wife and colleagues Thursday, McCarthy batted away a question on whether the letter influenced his decision.

“No,” McCarthy said, shaking his head. “No. No. C’mon.”

He would have won the majority of his Republican colleagues’ votes, but he expressed doubt about whether he could get to the 218 needed to win the majority.

He had lost the support of the tea party-affiliated House Freedom Caucus, and its conservative members backed another candidate.

“If we are going to unite and be strong, we need a new face to help do that. So nothing more than that. I feel good about the decision. I feel great to have my family here, my colleagues,” McCarthy said.