WASHINGTON — Walter M. Shaub Jr., the government’s top ethics watchdog who has repeatedly gone head-to-head with the Trump administration over conflicts of interest, said on Thursday that he was calling it quits.

Mr. Shaub’s five-year term as the director of the Office of Government Ethics is not set to expire until January, but with little chance of renewal and an appealing offer in hand from a nonpartisan advocacy group, he said the time was right to leave.

“There isn’t much more I could accomplish at the Office of Government Ethics, given the current situation,” Mr. Shaub said. “O.G.E.’s recent experiences have made it clear that the ethics program needs to be strengthened.”

The Run-Up

The podcast that makes sense of the most delirious stretch of the 2016 campaign.

His new position, he said, will allow him to advocate freely for such reforms.

In a short letter informing President Trump of his decision, Mr. Shaub did not offer a specific reason for his departure but extolled “the principle that public service is a public trust, requiring employees to place loyalty to the Constitution, the laws and ethical principles above private gain.” He had not been pressured to resign, he said.

In departing ahead of schedule, Mr. Shaub has handed Mr. Trump an opportunity to begin putting his mark on the agency overseeing the federal government’s vast ethics program, including that of the White House. The office’s director traditionally has had wide latitude to set its priorities, its tone in working with the White House and other federal agencies and, perhaps most important, how to interpret the country’s ethics laws.

The impending vacancy is all but certain to raise fears among Democrats and those in the small world of government ethics who see the office under Mr. Shaub as an important political bulwark against conflicts of interest in the upper echelons of the government. To Mr. Trump’s defenders, who have seen Mr. Shaub, an Obama appointee, as politically motivated, it is more welcome news.

The intensity of feeling over what is usually an obscure job speaks to the central role ethics have come to play in Mr. Trump’s Washington, where the vast holdings of the president and his cabinet, as well as an influx of advisers from businesses and lobbying firms, have raised a rash of accusations of conflicts of interest.

It is the job of the ethics office, a creation of a post-Watergate Congress, to work with a web of ethics officials at each agency to help those entering the government sidestep potential conflicts. The office guides each administration’s political appointees though financial disclosure requirements and creates agreements to restrict participation in deliberations over topics they handled for paying clients.

Mr. Shaub, 46, has faced an uncertain future at the agency since Mr. Trump took office in January. In the weeks between the president’s unexpected election victory and his inauguration, Mr. Shaub had taken an extraordinary gamble: He advocated very publicly on Twitter, and in a rare public speech, that Mr. Trump liquidate his vast business and personal holdings. The arrangement, Mr. Shaub argued, was the only truly ethical option.

Mr. Trump did not heed his advice, and by the middle of January, Mr. Shaub thought he might be fired. To minimize his attachment to the position, he packed up the personal possessions that filled his office.

But he was not fired, even as he continued to spar with Mr. Trump’s aides over a range of ethical concerns, including the ethics office’s authority to exercise oversight of the White House.

In February, he recommended that the White House discipline Kellyanne Conway, a top adviser to the president, after she made an on-air endorsement of the clothing line of Ivanka Trump, the president’s daughter. The White House Counsel’s Office disagreed and took no disciplinary action.

More recently, Mr. Shaub and the administration fought over a routine request by the ethics office for copies of waivers issued to White House appointees to work in the Trump administration. The Office of Management and Budget initially balked at the request, challenging Mr. Shaub’s legal authority even to ask for the information and asking him to withdraw it. After Mr. Shaub fired back with a stern 10-page letter shooting down the argument, the White House backed down.

The White House eventually released the waivers, showing it had granted at least a dozen exemptions for aides to work on policy matters they had handled as lobbyists or to engage with former colleagues in private-sector jobs. Mr. Shaub objected to the fact that many of the waivers were undated and unsigned, and that some gave approvals for acts retroactively.

Mr. Shaub, who served for about a decade as a career civil servant at the agency before being appointed director, said his role had always been politically neutral. Rather than fighting Mr. Trump, as some critics have suggested, Mr. Shaub has said his decisions to speak out have been motivated by a desire to defend an ethics program that traditionally has counted on support from Democrats and Republicans alike. He said he spoke publicly only after other more traditional channels were exhausted.

Mr. Shaub will leave the agency in this month to take up his new position as a senior director for ethics at the Campaign Legal Center in Washington, a nonpartisan group that advocates campaign finance reform and litigates voting rights cases. Mr. Shaub will have more freedom to comment on the government’s ethics program and propose changes to it.

A permanent replacement for Mr. Shaub would require confirmation in the Senate, where Democrats would probably use confirmation hearings to raise grievances about what they see as Mr. Trump’s own potential conflicts of interest.

The White House could also simply leave the position to an acting director indefinitely. Shelley K. Finlayson, the office’s chief of staff, is first in line for the acting director position, but the law governing such vacancies also allows the White House to choose from among the office’s upper ranks.