Two people have died and more than 50 have been arrested amid rioting and looting on the outskirts of South Africa's capital city of Pretoria.

Looters targeted shops in the Mabopane and Ga-Rankuwa neighbourhoods belonging to immigrants, echoing similar attacks last year against foreigners, including Pakistanis and Somalis who run businesses in poor urban areas.

Police fired rubber bullets to disperse crowds in Mabopane, where people ran out of one store with stolen groceries and other goods, according to South African media reports.

Two people were shot dead in the Mamelodi district.

Police arrested 54 people on charges of public violence, theft and possession of stolen property. The government said stability was returning to some areas, while violence persisted in other districts.

"The attacks and looting of shops show that some of the protest actions are motivated by pure criminality," the government said.

The violence started on Monday after the selection of the ruling party's mayoral candidate for Pretoria ahead of local elections on August 3. Some residents said they were not adequately consulted about the selection.

The rioting has raised concerns about security ahead of municipal elections on August 3.