Warsaw, Poland (CNN) Polish President Andrzej Duda has surprised observers by vetoing two bills that would have given his populist government sweeping powers over the country's courts.

The controversial bills are part of a package of judicial reforms put forward by the ruling right-wing Law and Justice Party (PiS) and passed by both houses of Polish parliament last week.

The proposed reforms have prompted huge protests in the capital Warsaw and across the country.

"This law would not strengthen the sense of justice" in society, Duda said in a televised declaration Monday, the AFP news agency reported.

Crowds began to gather outside the presidential palace on Monday morning, waving Polish flags and chanting: "we did it" and "thank you!" The President's veto is a victory for Poles who turned out to protest in over a hundred cities over the weekend.

Critics had condemned the legislation as a blatant power grab by the PiS, which repeatedly insisted that it was simply carrying out needed judicial reform.

Key to the legislation was that current Supreme Court judges would be forced into early retirement and new judges appointed by the Justice Ministry.

Protesters rally in front of the presidential palace in Warsaw on Sunday.

The legislation was described by protesters as the beginning of the end of democracy in the Eastern European country, one of the first former communist nations to join the European Union.

In his speech, Duda appealed to the opposition to "change tack," urging protesters not to turn to violence.

"People can demonstrate in the streets, can show their dissatisfaction, but not resort to violence," Duda said. "I admit, that my decision can be controversial. But, I'm stating here that this law, which has been passed through parliament, will not come into effect," he added.

A poll conducted by CNN affiliate TVN last week found that 55% of Poles said Duda should veto the court laws; 29% said he should not.

What's next?

Parliament has the power to override the President's veto, but it would require a three-fifths majority, which would be impossible for the PiS to achieve without the support of other parties.

The ruling party will now have to go back to the drawing board and re-envision its package of reforms.

Just before news broke that Duda would veto the legislation, Deputy Justice Minister Marcin Warchol voiced his concern that rejection of the bills could halt changes to a judicial system which he says is corrupt and inefficient.

Protesters rallying on Sunday against the proposed judicial reforms.

"If the President vetoes the law we will have to discuss the new shape of the disciplinary procedures against the judges," Warchol said.

"We can talk about the new law, but one thing is certain -- the ethical standards will have to be restored, and the Supreme Court must stand guard over those standards, instead of moving a corrupt judge to a different court."

Later on Monday, the President's spokesman confirmed that Duda would in fact sign a third bill, which will give the Justice Minister the power to select the heads of Poland's lower courts.

EU warning

The President's unexpected decision on Monday comes after both the European Union and US State Department voiced concerns over the legislation. The EU said that it would erode the judiciary's independence.

Frans Timmermans, first vice president of the European Commission, last week threatened the use of Article 7, a mechanism that would allow for sanctions against Poland and possible suspension of its voting rights in the bloc. The measure has never been used before.

Protesters hold posters reading "constitution" during a demonstration in front of the presidential palace on Sunday.

European Council President Donald Tusk, a former Polish Prime Minister from the opposition Civic Platform Party, warned last Thursday of "dangerous consequences" for Poland's standing on the world stage and said he had asked Duda for an urgent meeting.

"Bringing the courts under the control of the governing party in the manner proposed by the Law and Justice Party ... will ruin the already tarnished public opinion about Polish democracy," he said.

A Trump effect?

The public outcry over the plans had been partly overshadowed by recent high-profile visits to Poland by US President Donald Trump and the British royals.

But the US State Department tweeted a warning Friday to Poland "to ensure that any judicial reform does not violate (the) constitution & respects judicial independence."

US urges #Poland to ensure that any judicial reform does not violate constitution & respects judicial independence. https://t.co/TwkCcm8Jnu — Department of State (@StateDept) July 21, 2017

When asked whether the Trump administration would urge Duda to veto the measure, State Department spokeswoman Heather Nauert said: "I am not aware if we will ask him to do that."