In the weeks after Roger Ailes was ousted as the chairman of Fox News in July, amid a sexual harassment scandal, company executives secretly struck an agreement with a longtime on-air personality who had come forward with similar accusations about the network’s top host, Bill O’Reilly.

The employee, Juliet Huddy, had said that O’Reilly pursued a sexual relationship with her in 2011, at a time he exerted significant influence over her career. When she rebuffed his advances, he tried to derail her career, according to a draft of a letter from her lawyers to Fox News that was obtained by the New York Times.

The letter includes allegations that O’Reilly had called Huddy repeatedly and that it sometimes sounded like he was masturbating. He invited her to his house on Long Island, tried to kiss her, took her to dinner and the theater, and after asking her to return a key to his hotel room, appeared at the door in his boxer shorts, according to the letter.

In exchange for her silence and agreement not to sue, she was paid a sum in the high six figures, according to people briefed on the agreement. The agreement was between Huddy and 21st Century Fox, the parent company of Fox News. The company and O’Reilly’s lawyer said her allegations were false.

In the aftermath of Ailes’ departure, executives declared that such behavior would never again be tolerated. O’Reilly has continued to host his show on weekday nights at 8 p.m., and he published two more books.

Details about the allegations and the agreement between Huddy and 21st Century Fox are based on interviews with current and former Fox News employees, the letter written by her lawyers to the company, and three pages of a draft of the settlement agreement.

The letter was mailed anonymously in December to reporters for the Times; its authenticity was verified by several people who have been briefed on it. A person close to Huddy told the Times that she relayed accounts of O’Reilly’s unwanted advances at the time they occurred.

The website first reported the existence of the agreement Monday afternoon.

In the letter, lawyers for Huddy also said that a longtime Fox executive, Jack Abernethy, had retaliated against her professionally after she made clear that she was not interested in a personal relationship.

Representatives for Fox News and O’Reilly dismissed the allegations Monday night. “The letter contains substantial falsehoods, which both men have vehemently denied,” Irena Briganti, a spokeswoman for Fox News, said in a statement.

Speaking for O’Reilly, Fredric S. Newman, the TV host’s lawyer, said, “There is absolutely no basis for any claim of sexual harassment against Bill O’Reilly by Juliet Huddy.”

Ailes also denied the charges of sexual harassment that engulfed him and the network last summer.

Jeanne M. Christensen, a lawyer for Huddy at Wigdor LLP, declined to comment.

Huddy made her allegations known to Fox News in the letter her lawyers sent to the network in August. The settlement was reached on Sept. 5, at a time when the company was finalizing other agreements, including one with Gretchen Carlson, the former anchor whose sexual harassment suit forced the departure of Ailes.

The company declined to make a similar agreement with Andrea Tantaros, another on-air personality, after she raised similar complaints about network executives. As a result, Tantaros filed suit against the company in August.

O’Reilly has a towering presence at Fox News. He was close to Ailes, who ran the network with unchallenged authority, and his show generated about $180 million in advertising in 2015, according to Kantar Media, the ad-tracking firm.

He was also viewed by lower-level employees and program hosts as an influential figure in the newsroom; he was the gatekeeper to airtime on his show, and his support could be crucial to advancement.

But O’Reilly had run into problems before. In 2004, a producer on his show, Andrea Mackris, sued him, asserting that he had made unwanted sexual advances and lewd comments in a series of phone calls and dinner conversations. According to the suit, O’Reilly told her on multiple occasions to buy a vibrator, called her when it sounded like he was masturbating and described various sexual fantasies.

After two weeks of sensational headlines in New York’s tabloid newspapers, O’Reilly settled for millions of dollars, according to people briefed on the agreement. Both sides said that no wrongdoing had occurred.

According to the letter related to Huddy’s case, her lawyers said that O’Reilly began sexually harassing her in 2011. She started her career at Fox News in 1998 as a reporter based in Miami and went on to co-host a syndicated morning show in New York that was canceled in 2009. In the months afterward, Huddy tried to find a landing place at the network and appeared as a guest on O’Reilly’s show.

Huddy’s father, John Huddy Sr., who was a confidant of Ailes and a consultant to Fox News, left the network in July. Her brother, John Huddy Jr., continues to work there as a correspondent based in Jerusalem.

In January 2011, O’Reilly invited Huddy to lunch near his multimillion-dollar home in Long Island, New York, according to the letter. After lunch, he drove her back to his home, where he showed her every room, including his bedroom, and his collection of presidential memorabilia.

“To shock and disgust, as Ms. Huddy was saying goodbye to Mr. O’Reilly, he quickly moved in and kissed her on the lips,” the letter said. “Ms. Huddy was so taken aback and repulsed that she instinctively recoiled and actually fell to the ground. Mr. O’Reilly, looking amused, did not even help Ms. Huddy up.”

The next week, O’Reilly asked her to join him for dinner at the Harvard Club, followed by a Broadway show, according to the letter and to current and former Fox News employees.

Huddy was not interested in having a romantic relationship with O’Reilly but, the letter said, “she felt compelled to comply with Mr. O’Reilly’s request, given that he had total control over her work assignment.”

During the Broadway show, according to the letter, O’Reilly moved close to Huddy in a way that made her feel uncomfortable. He tried to hold her hand but she pulled it away. Then he dropped a key to the room at a Midtown Manhattan hotel he was staying at into her lap, and told her to meet him there after the show. He stood up and left, the letter said.

Huddy went to the hotel to return O’Reilly’s key, according to the letter. She asked him to meet her in the lobby, but he refused and asked her to join him in his room.

“Ms. Huddy declined and explained that she was not interested in Mr. O’Reilly on a personal or sexual level,” the letter said.

O’Reilly persisted and again asked that Huddy come up to his room, and she ultimately went up give him the key, according to the letter. It is not clear why she did not leave it at the front desk or simply leave.

When O’Reilly opened the door to his room, he was wearing nothing but boxer shorts, according to the letter. Huddy was “very embarrassed, handed Mr. O’Reilly his key and quickly left,” the letter said.

In the months after O’Reilly and Huddy went to the show in Manhattan, his “obsession with her only escalated,” the letter said.

O’Reilly started calling Huddy at all hours, even while he was on vacation. At times, the calls were about work, but they were sometimes “highly inappropriate and sexual,” the letter said. On some occasions, it sounded like O’Reilly was masturbating, the letter said.

“Disgusted, Ms. Huddy came up with an excuse and hung up the phone,” the letter said.

O’Reilly’s pursuit continued and Huddy tried to distance herself from him, it said. She answered and returned fewer of his calls.

“Ms. Huddy’s rejection of Mr. O’Reilly apparently did not sit well with him, as he began to retaliate against her both on and off air,” the letter said.

O’Reilly “nitpicked her work” and would “berate Ms. Huddy for minor mistakes,” according to the letter. O’Reilly stopped preparing her for segments and would surprise her with story angles that they had not discussed.

In 2013, Huddy was replaced on a segment she had on his show. Another segment that she was featured in, called “Mad as Hell,” was quickly canceled. She did not complain, fearing retaliation, she told current and former Fox News employees at the time.

The letter also included a series of accusations by Huddy against Abernethy, including that he started “trashing her” after she rejected his attempts to pursue a personal relationship. Abernethy signed a new multiyear contract with Fox News in September, after having been named co-president of the network in August.

As part of Huddy’s confidential agreement with 21st Century Fox, she agreed not to “disparage, malign or defame” the parties and the company, on its behalf and on the behalf of O’Reilly and Abernethy, agreed not to “disparage, malign or defame” Huddy.

The consequences for breaking the confidentiality of the agreement are severe, costing either side $500,000 per infringement, according to the document. Huddy is also liable if her lawyers or a person close to her discloses the terms of the agreement.

On Sept. 7, Huddy, who had hosted “Good Day Early Call” on WNYW, a Fox affiliate, went on the station and gave a tearful goodbye. “Thank you to everyone who has made the last 20 years the most challenging but best of my life,” she said. “Perhaps someday, even someday soon, you’ll see me on television again.”