Fox News Channel chief Roger Ailes officially stepped down on Thursday after two decades at the helm of the most successful cable news channel in history.

Ailes’ exit comes amid a sexual harassment probe sparked by a lawsuit filed two weeks ago by former Fox News anchor Gretchen Carlson.

Carlson accused Ailes, 76, of not renewing her contract as retaliation for her rejecting his sexual advances.

Ailes, who also resigned as chairman and chief executive of Fox Business Network, and as chairman of Fox Television Stations, has denied the allegations.

Fox News parent 21st Century Fox is conducting an investigation into Carlson’s allegations — and hired New York law firm Paul Weiss to head the inquiry.

That probe is winding down, sources said.

Rupert Murdoch, the executive chairman of 21st Century Fox, will assume the role of chairman and acting chief executive of FNC and FBN.

Both internal and external candidates are being considered for permanent roles, sources said.

Neither Ailes, through a spokesman, nor his lawyers were immediately available for comment.

In a letter posted on the Drudge Report, Ailes said he resigned because he didn’t want his situation to become a distraction.

Ailes, in the letter to Murdoch, said he is proud of his role in “advancing the careers of the many women I have promoted to executive and on-air positions.”

Despite his sudden departure, Ailes told Murdoch he is “proud of our accomplishments and look forward to continuing to work with you as an adviser in building 21st Century Fox.”

Murdoch, in a statement, said, “Roger Ailes has made a remarkable contribution to our company and our country. He shared my vision of a great and independent television organization and executed it brilliantly over 20 great years.”

Ailes defied the odds in creating a network that stood in contrast to the rest of the media landscape, Murdoch said.

Lachlan and James Murdoch, 21st Century’s executive chairman and chief executive, respectively, made clear they want to maintain a positive work environment.

“We continue our commitment to maintaining a work environment based on trust and respect,” the two said in a statement.

Shares of 21st Century Fox fell 8 cents on Thursday, closing at $27.72.

Fox News will take in about $2.5 billion in 2016, according to SNL Kagan estimates.

After Ailes’ yeoman-like effort, it is hard to remember that back in 1996, as it was launched, Fox News had to pay for carriage.

Today, it secures premium ad rates and big affiliate fees thanks to its colossal ratings and popular anchors.

Fox News held on to its No. 1 cable-ratings position for 14 years and broke records after the first Republican presidential face-off in 2015, netting 24 million viewers.

Ailes will become a personal adviser to Rupert Murdoch — not a consultant to the company, sources said.

When asked by The Hollywood Reporter what people might be surprised to learn about him, Ailes responded: “Most days I have a lot of fun in my job.”

21st Century Fox and News Corp., parent of The Post, share common ownership.