An Egyptian youth gestures during clashes between rival groups of protesters in Cairo, Egypt, on Friday.

CAIRO — Clashes erupted Friday between several hundred opponents and supporters of Egypt’s Islamist president during a rally by his allies calling on him to “cleanse the judiciary” of alleged supporters of the old regime.

Such violence has become an entrenched feature of Egypt’s stormy politics. In recent weeks, several marches and rallies by the various camps in the deeply polarized nation have devolved into street battles, fueling the bitterness on all sides.

Thousands of supporters of President Mohammed Morsi — mostly backers of the Muslim Brotherhood and other Islamists — held rallies outside the High Court building in Cairo and in the coastal city of Alexandria, demanding the “cleansing of the judiciary.” The marches come as the Islamist-dominated legislature announced plans to begin debating a bill regulating the courts.

The bill’s supporters present it as a way to ensure the judiciary’s independence. But opponents fear the Islamists aim to purge judges and install new ones supportive of their agenda to consolidate their hold on power. A senior Brotherhood figure has suggested a new law would force as many as a quarter of Egypt’s over 13,000 judges and prosecution officials into retirement.

As some Islamists at the rally moved toward Cairo’s Tahrir Square, they were met by anti-Morsi youth a few blocks from the square, some of them in masks. It was not clear who started the clashes, but it led to both sides pelting each other with stones. One bus was seen set on fire. The sound of birdshot cracked through the air in the clashes, and tear gas was fired — even though there were no police nearby.

Some of the masked youths and some Islamists were seen with homemade pistols. Others wielded iron bars and tree branches and broke up street pavements to throw the chunks of asphalt and concrete. At least 39 people were injured, according to the state news agency MENA.

Ahmed Hamdi, a Muslim Brotherhood supporter at the scene, blamed the anti-Morsi protesters for the violence, calling them “thugs” and saying they set the bus on fire.

“The whole story is they see that Islamists are now in power. They can’t swallow this, that Islamists rule them. It’s a battle with the old regime. The president must intervene to stop all this,” he said.

Police later moved in trying to break up the clashes, but the fighting moved into side streets. The Interior Ministry appealed to “all political forces” to withdraw from the street.

Egypt has been deeply divided for months over Morsi’s rule and the political dominance of his Islamist allies, leading to repeated violence even as the country’s economy continues to deteriorate.

Morsi’s opponents accuse the Islamists of monopolizing power, pushing through their own agenda, and allowing human rights abuses. The president, the Brotherhood and Islamist politicians say the opposition is using street violence to topple elected Islamists and destabilize the country.

The judiciary has been frequently dragged into the political confrontation.

Judges accuse Morsi of trampling on their authority, while the president’s allies charge that the judiciary is controlled by supporters of ousted President Hosni Mubarak trying to undermine Morsi and derail Egypt’s transition to democracy.

In November, Morsi infuriated many in the judiciary by issuing decrees that made his decisions immune from judicial challenge for a time, protected a constitutional assembly from being dissolved by the courts and unilaterally installed a new prosecutor.

The prosecutor remains in place despite a court order last month annulling his appointment.

The judiciary has dealt the Islamist camp several setbacks. Courts dissolved the Islamist-majority lower house of parliament last year, saying the law governing its election was invalid. This year, a court forced a delay in elections for a new parliament when it ruled that a new election law drafted by Islamists had to be reviewed by the Supreme Constitutional Court.

The election had been due to start this month but they have been put off with no new date set. In the meantime, the upper house of parliament — the Shura Council, a normally powerless body elected by no more than 6 percent of voters and where Islamists hold an overwhelming majority — is serving as the legislature.

The Muslim Brotherhood, from which Morsi hails, also criticized the courts over several recent acquittals of former officals from Mubarak’s regime as well as a court order to release Mubarak himself, who is being re-tried over charges of killing protesters during the 2011 uprising that led to his ouster.

Mubarak remains in detention on other charges and is unlikely to be freed.

The courts are the sole branch of government not under the dominance of Morsi’s Islamist allies, although he does have some backers among the judges.

Revolutionary groups have long called for reforming the judiciary, the Interior Ministry and other institutions to remove Mubarak holdovers. But they fear the Islamists will only purge the institution to install their own supporters.

Hamdeen Sabahi, an opposition leader, said Islamist claims about “cleansing” the courts are a prelude to “a new massacre of the judiciary.”

“We will support the independence of the Egyptian judiciary against any attempts by the executive to encroach on it.,” he wrote on his Twitter account.

On Friday, the head of the Brotherhood political party Saad el-Katatni denied the group was trying to monopolize power. The Brotherhood party “does not seek to govern alone, excluding other parties. Brotherhoodization of the state is a blatant lie.”

Friday’s rallies, which the Brotherhood backed, appeared aimed at showing popular support for Islamist moves to regulate the judiciary in the name of “the revolution.”

The Brotherhood’s secretary-general, Mahmoud Hussein, said earlier that the marches are pushing for implementation of “the demands of the revolution,” including legislation on the judiciary and “revolutionary measures” to purge state institutions of corrupt officials.

“Go for it Morsi and we are behind you. Cleanse the judiciary,” thousands of Morsi supporters chanted outside the High Court building in Cairo. “Hey, judge, who are you protecting, justice or the corrupt?”

In an interview with a Kuwaiti newspaper Al-Jarida this month, Mahdi Akef, the former supreme leader of the Brotherhood, said the judiciary “was corrupt” and has been antagonizing his group and Morsi.

“This is a sick judiciary,” he said in an audio recording that the paper put out from the interview. He said a new judiciary law “will toss out over 3,500 counselors and judges because they are over 60 years old.”

One proposal for reforming the judiciary has been to lower the retirement age for judges to 60 from the current 70 as a way to introduce new blood.

Nasser Amin, the head of the Arab Center for the Independence of the Judiciary, said reforming the judiciary does not mean purging it. Egypt is not Iraq where being a member of Saddam Hussein’s ruling party was a requirement for joining state institutions, so a purge is unnecessary, he said.

“We all call for reforming the judiciary, not controlling it,” he said. The aim of getting rid of so many judges at once would be “to control the whole system and use it against opponents.”

Apparently after an angry response from judges over the proposal, Justice Ministry spokesman Ahmed Rushdi said the ministry position is that the age of retirement is up to the judges to decide.

“It shouldn’t be used for political reasons, either by reducing it or increasing it.”