“The president did everything in his power to prevent the change,” said Sergiu Miscoiu, a professor of political science at Babes-Bolyai University in Cluj. He said the president made the correct choice to avoid delegitimizing the governing system and prompting further crises.

That is unlikely to appease many of his supporters, however.

“Iohannis has still to convince them that his stance is the only reasonable one,” Mr. Miscoiu said. “This can be done only by trying to impose a replacement who is on the precise same line as Kovesi. A regular, tough, preferably low-profile, prosecutor with a good record of convictions, and with no major liability. It’s not easy to find but there are some.”

In an interview in her office last month, Ms. Kovesi told The New York Times that if the independence of the prosecutor disappeared, “as it appears it now does, the job does not become more difficult,” it becomes impossible.

At a news conference after her dismissal was announced, Ms. Kovesi thanked her colleagues and said she would be leaving the anticorruption agency but would remain a prosecutor. “What we have succeeded in demonstrating is that Romanian public institutions are working legally and yes, corruption can be defeated,” she added. “Today’s episode is not a defeat.”

The president’s decision comes at a crucial moment for rule of law in Romania. On June 21, Liviu Dragnea, the powerful leader of the Social Democratic Party and the speaker of the lower house of Parliament, was found guilty of abuse of office, for intervening to keep two of his party’s employees, who performed no state work, on the public payroll from 2006 to 2013, when he was a local council leader.

The court sentenced Mr. Dragnea to three years and six months in jail, though the verdict is expected to be appealed. Mr. Dragnea was previously given a two-year suspended sentence for electoral fraud.

Last week, Parliament rushed through changes to the country’s criminal code overhauling abuse-of-power laws. Mr. Iohannis has said he will challenge the legislation at the Constitutional Court.