It all began with the arrest of a rape suspect in an open-air market two weeks ago. A brawl erupted. A police officer was injured.

It was a minor, if ugly, episode that might quickly have been forgotten except that it snowballed into a sweeping government campaign not against crime in the city’s markets, but against illegal immigrants, though the suspect was not an immigrant at all.

In the days that followed, the police and migration officials mounted raids at markets across Moscow, in factories that operated in the shadows of the law, in the city’s subway system and on the streets. At last count nearly 1,500 foreigners had been detained, according to the Federal Migration Service. That number included 586 people, most of them Vietnamese, who were being held in a temporary tent camp more appropriate for a war zone or the scene of a natural disaster than the center of a capital city.

“This is absolutely normal,” Moscow’s mayor, Sergei S. Sobyanin, told the newspaper Vedomosti last week, defending the government’s actions. “In any society, in any country, if an emergency situation happens, then the government and society begin to act more harshly.The campaign — cheered, for the most part, by the news media and the public here — has exposed the complexity and corruption of Russia’s labor market and tapped into the country’s ever-simmering ethnic animosity. And that has raised concerns among foreign embassies and provoked outrage from national and international human rights groups.
Svetlana A. Gannushkina, the director of the refugee-rights group Civil Assistance, denounced the camp as “an illegal place of detention” that had ensnared innocent people, even those with permission to live and work in Moscow.

The Vietnamese Embassy sent its diplomats to the camp on Friday — a week after it first opened — to try to resolve the matter and begin to identify those being held. Many did not have their passports when the police raided a shadowy textile factory near where the tent camp appeared, and speak little Russian.

Human Rights Watch on Friday called conditions in the camp inhuman and demanded that the authorities close it and end a campaign it said was aimed at people based on the color of their skin, not their nationality. Those being held include people from former Soviet republics with close ties to Russia, like Uzbekistan, Kyrgyzstan and Tajikistan, as well as Syria, Afghanistan and Egypt.
“Everything about this massive sweep violates Russia’s obligations under international law,” Human Rights Watch’s director in Russia, Tanya Lokshina, said in a statement. “Prolonged detention without counsel, ethnic profiling, inhuman conditions it should stop now.”

There is little evidence that it will end soon. The first 31 of the Vietnamese workers were deported over the weekend. The head of the Vietnamese Embassy’s consular division, Lee Hong Chung, said in a written response to questions about the detentions that the embassy was working closely with the Russian authorities to resolve the fate of the others.

The raids, which some critics have called “zachistki,” a word for cleansing operations that gained currency during Russia’s war in Chechnya, continue almost daily, supported by a large majority of Russians, nearly two-thirds of whom think immigrants increase crime and corruption, according to the results of a recent poll.
Not surprisingly perhaps, the scourge of illegal immigration has been taken up not only by Mr. Sobyanin, but also by his challengers across the political spectrum in the mayoral election to be held on Sept. 8.
The most prominent challenger is Aleksei A. Navalny, the anticorruption activist and champion of a more democratic political system, whose remarks have unnerved more liberal members of the political opposition. He has frequently stated that half of all violent crimes are committed by immigrants, a figure that is disputed. “For me this isn’t just a number,” Mr. Navalny said in a recent stump speech. “For me it means one simple thing: that the women in my building are afraid to go out on the street at night.”
Campaigns against migrant or immigrant workers happen so routinely here they seem seasonal. The issue, not unlike the debate over immigration in Europe or the United States, is further inflamed here by widespread racism toward Muslims from the provinces of the Northern Caucasus, which are part of Russia, and from the former Soviet republics of Central Asia, which are not.
The arrest of a Russian in Tajikistan in 2011 led to a similar sweep against Tajik workers across Russia.
Often the raids coincide with politics inside Russia, and any subtleties about the issue — like the fact the suspect in the rape was a lawfully registered worker from Dagestan in southern Russia are lost in the public outcry.
“They are basically exploiting the xenophobia that is so prevalent in Moscow,” Ms. Lokshina of Human Rights Watch said in an interview, blaming the election campaign for the fervor of the latest crackdown. “I believe it’s quite cynical.”
Mr. Sobyanin, appointed mayor in 2010 when the president selected regional leaders, has made cleaning up Moscow’s bazaar-like markets a signature policy ahead of his first electoral test as mayor. In the days after the attack he appeared at a televised meeting with President Vladimir V. Putin and said the city had closed 30 markets in the past two years in an effort to impose order on the retail industry.

The brawl that precipitated the crackdown, he told the newspaper Vedomosti, “was the last drop, which overfilled the cup of patience of the security services, and they are beginning to react more harshly.”

Mr. Putin said that neither race nor religion had any relevance in the campaign, but added that the government had to do more to combat the sort of lawlessness in which “a police officer has his head fractured.”
The markets, which emerged in the gray shadows of capitalism after the collapse of the Soviet Union, have long been associated with the underground economy, controlled by gangs and officials who exploit the vulnerability of illegal workers by withholding pay or having them arrested. “These people are a part of the system of illegal slave labor that has appeared in Russia,” Ms. Gannushkina said at a briefing on Wednesday. “They are the victims of this system, the first victims.”
At the same time the workers are a source of cheap labor in the retail trade and construction industries, and thus a vital part of the country’s economy. Human Rights Watch has chronicled abuses at companies building the facilities for the Olympic Games to be held in Sochi in February.
The hundreds of Vietnamese were detained during a raid at an underground textile factory not far from Cherkizovsky Market, which the authorities shut down in 2009 after citing health and safety violations. “Counterfeit products were being produced on three underground floors,” Mr. Sobyanin said during his televised meeting with Mr. Putin. “In other words, this business was flourishing. Today, all this was removed from there.”
The workers have proved more difficult to remove. So many have been detained in the past two weeks that they have overwhelmed the city’s existing detention centers. The Emergency Services Ministries erected the tent camp to accommodate the spillover. The detainees can be held there as long as three months under the law, though officials said this weekend that they hoped to close the camp as soon as possible.

At least some detainees have appeared in court, though many lack documents, complicating the effort to establish whether their status is illegal or not.
One detainee, Abdul Khamid al-Badri, who is from Egypt, said he was in Russia legally on a student visa when the police detained him near a subway station. He had no phone and no passport with him at the time. His immediate fate remains uncertain nearly two weeks after his detention.
“The judge told me I would have to go home,” he said, speaking English as he described a Kafkaesque detention. “I just want my freedom. Where is it here? I can’t even go home.