The president of Poland wielded his veto power on Monday to pull the nation back from further undermining the independence of its judiciary, rejecting two measures that European officials — and tens of thousands of protesters — have condemned as a danger to democracy and the rule of law.

The decision of the president, Andrzej Duda, opened an unexpected new chapter in a debate that has riven Poland, once a model of post-communist democracy in Eastern Europe.

His morning announcement came as a surprise, and a setback, for the ruling right-wing Law and Justice party, which came to power in 2015.

Prime Minister Beata Szydlo said the president’s decision was “incomprehensible.”

But Lech Walesa, a former leader of Solidarity who won the Nobel Peace Prize and went on to serve as Poland’s first president after the downfall of communism in 1989, called the surprising veto “difficult and brave.”

People gather in front of the presidential palace on Monday during a protest against the Supreme Court legislation in Warsaw. (Reuters)

The leader of the Law and Justice party, and the architect of efforts to bring the judiciary to heel, is Jaroslaw Kaczynski, 68. He had chosen Duda, 45, to run for president in 2015. Before that, his late brother, Lech Kaczynski, had mentored the younger man, at one point calling him his “son.” The relationship led many to believe that Duda would fall in line, as he had done previously at crucial moments when the populist party had moved against Poland’s independent institutions.

But Duda, who is trained as a lawyer, was apparently troubled by aspects of the legislation, and faced fierce domestic and international pressure to resist Law and Justice. He said he would rewrite the legislation over the next two months, in consultation with judges and nongovernmental groups.

“Poland needs a reform, but a wise one,” Duda said at a morning news conference, adding that he made his decision after conversations with lawyers and politicians but also sociologists and philosophers. “I am aware I will be criticized, probably by both sides of the political scene, but I make my decision with great responsibility for the Polish state.”

Later Monday, in an address from the presidential palace, he said the judiciary must remain independent but pledged reform. Without changes, he said, “there is no way to build a just state.”

One of the measures he quashed, which had cleared Parliament just two days earlier, would have removed all current justices of the Supreme Court except those handpicked by the governing party’s justice minister. The other would have given lawmakers authority over appointments to the National Council of the Judiciary, currently an independent body that names judges to the nation’s courts. Duda let stand a measure that gives the justice minister more control over local courts.

To override the president’s veto, Law and Justice, which has only a slim majority in Parliament, would need to muster the support of 60 percent of lawmakers, making such a move unlikely.

Party members huddled Monday at their headquarters to chart a path forward. In an evening address, Szydlo said that instead of meeting with lawyers, the president should “listen to the voices of ordinary Poles.”

Duda gave several reasons for his veto, saying that he had not been properly consulted and that such drastic changes had not been part of Law and Justice’s election platform. He also said the restructuring handed too much power to the justice minister, Zbigniew Ziobro, who also serves as the government’s prosecutor.

“We don’t have a tradition that the general prosecutor can interfere in the Supreme Court’s work,” Duda said.

His thinking, he said, had been informed by talks with Zofia Romaszewska, who was active in Solidarity, the Polish labor union that helped bring down communism across Europe. He said she told him, “Mr. President, I have lived in a country in which the general prosecutor could do anything, and I would not come back to it.”

Bolek Matuszewski, a private attorney in Warsaw, said the ­president’s feeling that he was left out of the process was compounded by the fact that the changes increased the authority of the justice minister at the expense of his own. “There were also plainly unconstitutional elements,” Matuszewski said.

Opposition leaders hailed Duda’s stand but said he did not go far enough in turning back Law and Justice’s efforts to paralyze the courts. The president’s veto was the last hope for opponents of the changes, as the Constitutional Tribunal, the body with the authority to invalidate the legislation, has already been remade to reflect government interests.

“It is a step in the right direction and recognition for the protesters,” said Kamila Gasiuk-Pihowicz, a leading member of the Modern party. “Now we need consultation and negotiations, not this hasty legislative process.”

Walesa called on Poles to continue demonstrating against the third measure.

Duda was pulled in opposite directions. As activist groups threatened to intensify their protest if he did not also veto the third measure, the president met with top Law and Justice officials, including the prime minister. But his spokesman said he would not change his mind.

The Supreme Court’s chief judge, Malgorzata Gersdorf, who also met Monday with Duda, said afterward that she was ready to cooperate in drafting changes to the judiciary.

“Without a doubt, the judiciary requires reform,” Gersdorf said in an interview Friday, citing the need to steer more litigants to arbitration. But there was no evidence, she maintained, of systematic corruption, the charge leveled by Law and Justice. The voters who have been convinced otherwise, she said, are those who have not seen their lives improve since the fall of communism in 1989.

“The philosophy of social attention that inspired Solidarity fizzled,” she said.

At the same time, she said, mass demonstrations made clear that many people continue to hold the courts in high regard. Polling showed that a majority of the country wanted Duda to reject all three bills.

Popular opposition to the legislation was amplified by warnings from Brussels, where European leaders pushed hard against what they said was a threat to the rule of law in a nation once held up as a post-communist success story. Poland joined the bloc in 2004, part of the expansion that included much of Eastern Europe.

Perhaps even more significant was the warning that came from the United States, said Bartosz Weglarczyk, director of Onet.pl, a major Polish news portal. Two weeks after President Trump visited Warsaw and extolled the government’s nationalist leaders, without mentioning the judicial overhaul, the State Department said Friday that it was concerned about the rule of law in Poland.

“That was probably one of the key elements of the pressure,” said Weglarczyk. “But it all came together over the weekend — the demonstrations, the calls by politicians and judges, the demands from universities, the international pressure. Truthfully, I’ve never seen a Polish politician under such huge pressure.”

Stanley-Becker reported from Berlin. Magdalena Foremska in Warsaw contributed to this report.

Read more:

Poland’s long march toward democracy is threatened by quick steps away from it

It only took two years for a ‘robust’ European democracy to fall apart

Led by populist party, Polish Parliament moves to strip Supreme Court of independence

Today’s coverage from Post correspondents around the world

Like Washington Post World on Facebook and stay updated on foreign news