Two days after escaping a roaring blaze by slithering down a curtain with his 15-month-old daughter strapped to his chest, and hours after burying two fellow Malawians who didn’t survive, Yasini Kumbasa was stopped in downtown Johannesburg by police officers demanding to see his passport.

He’d lost just about everything in the fire, but the officers were unmoved when he tried to explain that his passport was destroyed. Accusing him of being in South Africa illegally, they locked him up and demanded at least 1,500 rand, or $78, about what he paid in rent each month, for his release, Mr. Kumbasa said.

After spending three nights in a downtown police station, Mr. Kumbasa, 29, said he got out with money his wife borrowed from a Malawian acquaintance.

As South Africans furiously debate the decades of failed government policy, overlooked warnings and ineffective leadership that led a derelict building occupied by hundreds of squatters to go up in flames last week, immigrants again find themselves in the cross hairs and feeling more vulnerable, even as they carry the heaviest trauma from the blaze.

The authorities have not released the identities of the 77 confirmed dead, but interviews with residents of the building and aid groups suggest that most of the victims — most residents, in fact — were natives of other African nations.

Many of the immigrants who escaped the flames but lost loved ones have avoided government shelters and public hospitals, fearing that immigration officials might check their legal status and deport them if all their papers aren’t in order.

Immigration stops and extortion attempts that immigrants say the police frequently carry out in Johannesburg have become even scarier, especially for those like Mr. Kumbasa who lost their passports in the conflagration.

Concerns are building as well about anti-immigrant rhetoric and violence.

With national elections looming next year, some politicians have seized on the tragedy to lash out at migrants, whom they blame for fueling a housing shortage and preventing officials from cleaning up squalid buildings. Some are calling for tighter border controls — potentially a winning message in a country where half of the population says that foreign nationals should not be allowed to work because they take jobs from citizens.

As one of Africa’s economic powerhouses, South Africa has long been a magnet for migrants from desperately poor countries throughout the continent’s southern region. Yet after arriving, immigrants find themselves living a precarious existence, violently attacked at times and blamed for intractable problems like crime, joblessness and a housing crisis.

After the tragedy, officials with the Department of Home Affairs, which enforces immigration laws, were quick to show up at emergency shelters, as many of the survivors feared. But Johannesburg city officials said they were there only to help with missing documents, for both immigrants and citizens, not to deport people.

Colleen Makhubele, the speaker of the Johannesburg City Council, said the city was focused on addressing the immediate humanitarian crisis and was not seeking documentation from immigrants affected by the fire.

But, “we can’t suspend the law forever,” she said in an interview, suggesting that survivors who want proper documentation seek government help in getting it — even if that means returning to their native countries and applying for visas from there. For now, though, the shelter is the safest place for undocumented immigrants, she said.

“In the streets, we can’t control who’s going to pick them up,” she said. “When the policeman comes, he just wants his documentation. If you don’t have it, they don’t care whether you jumped off a building or not. They’ll just take them in.”

Immigration enforcement has become a routine part of policing in South Africa. Though courts have rejected the practice of indiscriminately stopping people suspected of being in the country illegally, immigrants say that police officers regularly demand documentation from them on the streets.

Violence is another ever-present threat to migrants. In Diepsloot, a township north of Johannesburg, South African residents blamed a spate of violent crime last year on foreign nationals, and a Zimbabwean man was burned to death by an enraged mob.

In response, the law enforcement authorities launched broad immigration sweeps through the township. For several weeks, police officers, accompanied by Home Affairs officials, patrolled the streets, grabbing men at outdoor markets and other public venues, demanding to see their papers.

If they could not produce them, they were thrown into police vans and taken to jail. News outlets reported that officers sometimes asked people to say words in local languages to test whether they were South African.

Sultan, a native of Tanzania, said he had never experienced that sort of police action in a decade of living in South Africa — until this week, after he escaped the deadly fire but lost his convenience store on the building’s ground floor.

A few days later, he was going to get something to eat when two police officers asked for his passport.

Sultan, 43, who asked that his last name be withheld out of fear of further trouble, told them it had been destroyed in the fire, and they put him in the back of their van. They told him that if he paid them 1,500 rand, they would release him, he said, otherwise they would take him to a deportation center.

The officers drove around for several hours with him and other immigrants they arrested, he said. Eventually, Sultan was released after a friend brought the money to pay the officers.

Brigadier Brenda Muridili, a spokeswoman for the South African Police Service in Gauteng, the province that includes Johannesburg, said the department took “any allegation of corruption seriously.” Police officials have received complaints of officers extorting foreign nationals, she said, but the challenge is that accusers often do not want to cooperate with police investigations.

Much of the attention on xenophobia in South Africa has focused on occasional violent outbursts against foreign-born residents. But in recent years, anti-immigrant sentiment has manifested itself in government policy and rhetoric. Officials have restricted some pathways to legal residence, moved to limit job opportunities for immigrants and ordered more aggressive measures to round up those who may be residing in the country illegally.

A provincial health official was captured on video last year berating a Zimbabwean woman at a hospital, accusing her of contributing to overwhelming the country’s health system.

Several people injured in the fire hesitated to seek medical treatment, fearing contact with the authorities.

Twenty-two-month-old Happiness Mwanyali was badly burned along her right thigh as her mother, Mary Sosa, carried her on her back to escape the building. But Ms. Sosa, 36, from Malawi, hesitated to take her daughter to the public clinic where they are usually treated, because all of her immigration documents had been destroyed. Without those, she said, she feared the clinic might not serve her and that Home Affairs might come to deport them.

So the day after the fire, she tried a treatment friends had suggested: rubbing toothpaste on the wound.

Happiness, who has soft cheeks and curious eyes, eventually got medical care from a private clinic when a nonprofit organization intervened to help.

“As foreigners, we don’t live freely,” said Ms. Sosa, who has lived in South Africa for three years, and sells peanuts and bananas on the street. “We live by hiding away from the police. It’s a painful way of living, but I don’t have a choice because that’s how we hustle.”

That bargain, trading a bit of freedom to earn a living, is one that some immigrant survivors of the fire say they are reconsidering.

Even though Mr. Kumbasa said he did not make that much in the odd jobs he worked in South Africa, life here had been better than it was in Malawi, where he could not earn a living. But getting arrested, after losing so much in the fire, has shattered his sense of security in South Africa, he said.

It is time, he said, to move back to Malawi.