Police fired teargas and detained hundreds of activists as more than 20,000 demonstrators gathered across Malaysia's capital on Saturday, demanding electoral reforms in the country's biggest political rally in years.

The opposition-backed rally was the culmination of weeks of intense pressure on the government of prime minister Najib Razak to make election laws fairer and more transparent before general elections expected to take place by mid-2012.

Demonstrators marched in defiance of Najib's administration, which has declared the rally illegal and warned people to avoid it.

Opposition leaders accuse Najib's National Front coalition of relying on fraud to preserve its 54-year grip on power, which has been eroded in recent years amid allegations of corruption and racial discrimination. The government insists the current electoral policies are fair.

Authorities took extraordinary security measures to deter the rally by sealing off roads, closing train stations and deploying trucks with water cannons near the Independence Stadium in central Kuala Lumpur, where activists sought to gather.

Police said in a statement they had detained 924 people, including senior opposition officials, in what they called Operation Erase Bersih, referring to the Bersih (Clean) coalition of groups behind the rally.

Thousands tried to reach the stadium from various parts of Kuala Lumpur, chanting "Long live the people", and carrying yellow balloons and flowers as they marched.

Police fired numerous rounds of teargas and chemical-laced water in repeated attempts to disperse the crowds, causing demonstrators to scatter into nearby buildings and alleys before regrouping. Police helicopters flew overhead as a brief downpour failed to deter the protesters.

The demonstrators finally dispersed after a five-hour standoff with police. Only several hundred reached the stadium.

Najib insisted on Saturday that the protesters represent a minority, and that most Malaysians support his administration. "If there are people who want to hold the illegal rally, there are even more who are against their plan," the prime minister was quoted as saying by the national news agency, Bernama.

Witnesses said riot police armed with batons charged at some protesters and dragged them into trucks. Some were seen bleeding, but police could not confirm any injuries.

The opposition leader Anwar Ibrahim said on Twitter that he had sustained a "minor injury" when his group was hit by teargas. The Malaysiakini news website said he had a knee injury.

The crackdown "stirred a sense of outrage against the exhibition of raw power by our government", the Bersih coalition leader Ambiga Sreenavasan told reporters. "What is the necessity for a show of might against right? No matter what, right will always prevail," she said, minutes before police detained her and other Bersih officials.

Activists estimated that the total number of demonstrators exceeded 20,000 people, making it Malaysia's biggest street rally since 2007. Some independent news websites estimated there were tens of thousands of people, but authorities did not immediately have an official figure.

The rally has galvanised the opposition and has been credited for a surge in political awareness among the public in recent weeks. Meanwhile, government officials accuse Anwar's three-party alliance of endorsing the rally to cause chaos on the streets and undermine the National Front.

Over the past two weeks, more than 200 other activists have been arrested nationwide for trying to promote the rally. Six are being held under security laws that allow indefinite detention without trial. Most of the others have been released, but some have been charged with laws banning activities linked to illegal assemblies. They face several years in prison if convicted.

The activists' demands include an overhaul of voter registration lists, tougher measures to curb fraud and fairer opportunities for opposition politicians to campaign in government-linked media. The National Front's mandate expires in mid-2013 but many analysts expect elections to be called by next year.