TRIPOLI, Libya — As rebel leaders pleaded with their fighters to avoid taking revenge against “brother Libyans,” many rebels were turning their wrath against migrants from sub-Saharan Africa, imprisoning hundreds for the crime of fighting as “mercenaries” for Col. Muammar el-Qaddafi without any evidence except the color of their skin.

Many witnesses have said that when Colonel Qaddafi first lost control of Tripoli in the earliest days of the revolt, experienced units of dark-skinned fighters apparently from other African countries arrived in the city to help subdue it again. Since Western journalists began arriving in the city a few days later, however, they have found no evidence of such foreign mercenaries.

Still, in a country with a long history of racist violence, it has become an article of faith among supporters of the Libyan rebels that African mercenaries pervaded the loyalists’ ranks. And since Colonel Qaddafi’s fall from power, the hunting down of people suspected of being mercenaries has become a major preoccupation.

Human rights advocates say the rebels’ scapegoating of blacks here follows a similar campaign that ultimately included lynchings after rebels took control of the eastern city of Benghazi more than six months ago. The recent roundup of Africans, though, comes at a delicate moment when the new provisional government is trying to establish its credibility. Its treatment of the detainees is emerging as a pivotal test of both the provisional government’s commitment to the rule of law and its ability to control its thousands of loosely organized fighters. And it is also hoping to entice back the thousands of foreign workers needed to help Libya rebuild.