The Washington Post's Paul Farhi explains what's next for Fox News and the Murdoch family, now that Chairman and CEO Roger Ailes is out. (Peter Stevenson,Jorge Ribas/The Washington Post)

Roger Ailes, the founder and chief executive of Fox News, has resigned from his position under fire, but will remain with the company until 2018 as a consultant, Fox News’s parent company, 21st Century Fox, said Thursday.

Ailes will be replaced temporarily by Rupert Murdoch, the chairman of 21st Century Fox, the company said. The resignation marks the sudden and swift end to the 20-year reign of one of media’s most powerful moguls, a man who micromanaged talent acquistion at Fox, shaped its conservative viewpoint, and turned it into a ratings juggernaut.

Ailes’s job has been in question for the past week or so, amid a budding sexual harassment scandal. The powerful executive was accused of harassment in a lawsuit filed two weeks ago by former network anchor Gretchen Carlson, who said Ailes pressured her for a sexual relationship in exchange for keeping her job as a host of a Fox News program. Since then, other women have come forward with allegations against Ailes, 76, some stretching as far back at the mid-1960s. Ailes has repeatedly denied the allegations.

People familiar with Ailes’s exit negotiations said he will receive around $40 million, much of which 21st Century Fox was already obligated to pay him under a severance package.

Roger Ailes has stepped down as Fox News chairman and chief executive amid a sexual harassment suit brought forward by former host Gretchen Carlson. (Erin Patrick O'Connor/The Washington Post)

In a statement, Murdoch said: “Roger shared my vision of a great and independent television organization and executed it brilliantly over 20 great years. Fox News has given voice to those who were ignored by the traditional networks and has been one of the great commercial success stories of modern media.” Murdoch’s statement made no mention of the recent allegations against Ailes.

In his resignation letter to Murdoch, Ailes said, “I will not allow my presence to become a distraction from the work that must be done every day to ensure that Fox News and Fox Business continue to lead our industry.” He also expressed “pride in the role that I have played advancing the careers of the many women I have promoted to executive and on-air positions. Many of these talented journalists have deservedly become household names known for their intelligence and strength.”

Fox News anchor Chris Wallace, in Cleveland to cover the Republican National Convention, said he was stunned by the news of Ailes’s resignation. “In almost 50 years in the news business, Roger Ailes is the best boss I have ever had,” he said. “I admire him professionally. And I love him personally. I am heartbroken he is gone.”

While Murdoch, 85, reportedly sided with Ailes, Murdoch’s sons — Lachlan and James — were long at odds with him and saw the sexual-harassment allegations as a pretext to remove him. Lachlan, 44, is 21st Century’s executive chairman and James is its chief executive. In a joint statement, Lachlan and James Murdoch made an oblique reference to the scandal, citing their commitment to a respectful workplace.

They said: “We join our father in recognizing Roger’s remarkable contributions to our company. Our talented Fox News and Fox Business colleagues . . . have built something that continues to redefine the cable news experience for millions of viewers. We are enormously proud of their accomplishments. For them, as well as for our colleagues across our entire organization, we continue our commitment to maintaining a work environment based on trust and respect.”

Carlson’s attorney, Nancy Erika Smith, said in a statement that it took only two weeks after the former Fox host’s lawsuit to cause “a seismic shift in the media world. We hope that all businesses now understand that women will no longer tolerate sexual harassment and reputable companies will no longer shield those who abuse women.”

Ailes was named founding chief executive of Fox News in 1996 and, with a company reorganization in 2005, took on the role of chairman. He is credited with driving the top ratings of the network, which re-created TV news by catering to, and developing, a vast conservative audience. Fox News is the highest-rated cable network and reportedly has profits of well over $1 billion.

Prior to his time at Fox News, Ailes was a media consultant for Richard M. Nixon, Ronald Reagan and George H.W. Bush, and for Rudolph W. Giuliani in his first campaign for New York mayor.

Among the provisions in the exit agreement is a non-compete clause, which would prevent Ailes from starting a competitor to Fox News. As Ailes’s fate hung in the balance over the past week, there was speculation that he might raid some of the personalities that he recruited to Fox, including stars Bill O’Reilly and Sean Hannity, upon his departure. But the non-compete agreement effectively ends that possibility. People close to the company said Ailes will not have a “formal” role as a consultant — that is, in producing programs or news segments. Instead, he will advise Rupert Murdoch during a transition period.

Ailes’s fall played out in the news media that Ailes — and Fox — had sought to be an alternative to. Given that Ailes was a few months from the end of his current contract, the Murdochs could have let him slide into retirement rather than make an issue of the allegations that Carlson raised. Instead, in an echo of the phone-hacking scandal that beset the Murdochs’ media and entertainment empire in 2012, the publicity surrounding the allegations apparently built pressure on the family to act.

The law firm Paul, Weiss was in the middle of conducting an internal review of complaints of sexual harassment against Ailes and an alleged culture of sexism within the company when the younger Murdochs began to press for Ailes’s removal last week.

Margaret Sullivan contributed to this report.