On the roofs of the highest buildings in Tajoura, a military complex and migrant detention centre in southern Tripoli, snipers are taking position.

“Tonight nobody will sleep because of fear,” said a refugee locked up there. “We can hear the sound of guns and explosion of bombs very close to the detention centre.”

Scores of people died in fighting in Libya since the renegade military leader General Khalifa Haftar ordered his Libyan National Army forces to advance on the capital, where the UN-backed Government of National Accord sits, earlier this month. On Monday Ghassan Salamé, the UN’s special envoy for Libya, accused Haftar of mounting a coup attempt.

As the fighters controlling Tajoura prepare for a showdown, some of the hundreds of refugees and migrants inside the complex, and in other detention centres across Tripoli, are worried that they will become victims and targets, after they were forced to take an active role supporting militias aligned with the GNA. Compelling detained refugees and migrants to support fighters could constitute a war crime.

Since Haftar’s advance began on 4 April, migrants and refugees in three detention centres have told the Guardian they were taken out of locked halls and ordered to carry and move weapons, with some transferred to military bases around the city. In a fourth centre, detainees said people had been taken away by fighters, but they don’t know what’s happened to them.

“The police and soldiers force migrants to do cleaning work and load weapons. They [tell] migrants that if you know how to shoot a gun we will make you stay with us,” said one migrant who messaged the Guardian using a hidden phone.

The Guardian has also been sent photos of migrants wearing what appear to be military uniforms.

“We even washed the cars that were full with the blood of the soldiers who were killed on the war front,” said another detainee. “They put the dead bodies in the military cars. I’m not feeling good but I don’t have a choice here.”

“Forcing civilians to stockpile weapons in a warzone counts as abusive forced labour, is unlawfully cruel, and puts them at unnecessary risk,” said Judith Sunderland, associate director for the Europe and Central Asia division of Human Rights Watch. “Making them wear uniforms suggests they are being used as hostages or human shields, both of which are war crimes.”

“Far from pressing detained migrants into these dangerous activities, all parties should be taking all possible measures to avoid civilian casualties. The authorities should release all migrants who are arbitrarily detained and ensure their safety.”

About 6,000 refugees and migrants are currently locked up in detention centres ostensibly under the control of Libya’s directorate for combating illegal migration, which is associated with the government of Fayez al-Sarraj.

In reality, however, many of the centres are run by militias. The directorate did not respond to multiple requests for comment.

The roughly 600 detainees in Tajoura are among tens of thousands of men, women, and children who have been intercepted in the Mediterranean sea trying to reach Europe over the past two years, and brought back to indefinite detention. The EU is spending tens of millions of euros to train, fund, and equip the Libyan coastguard, in a bid to decrease migration from Africa.

“They know what is going on in the Libyan detention centres but the EU pretend like they don’t know,” said one detainee. “Libya is not the right place or safe place for refugees and migrants to stay. They should stop bringing back those who tried to cross the sea.”

A detainee in another Tripoli centre said: “Libya is a market of human beings. People are still working by slaving in Libya, so how can people stay in Libya?”

Before the escalation of fighting over the past 11 days, more than a dozen current and former detainees spoke to the Guardian about their experiences of forced labour. Sometimes they worked for Libyan guards in the detention centres, and sometimes for people associated with them, including relatives.

Refugees and migrants said they have cleaned houses, constructed buildings and toiled on farms.

“As a detainee it is a great opportunity [to] escape the stuffy prison, or at least you got to see the sun and also fresh air while working enslaved,” said an Eritrean who was caught at sea in May 2018, and escaped to Tunisia at the end of last year.

Women didn’t have to leave the centres to work but some minors did, he said. “Even underage [people] go to that enslaving labour.”

Detainees had been forced to work for four or five months straight, another man claimed, saying he had built houses for guards working for the directorate for combating illegal migration. “If I stop working or take a rest they will start shouting in a threatening way that scares me … If migrants try to escape, they’re going to shoot them.”

Sometimes workers are rewarded with cigarettes, bread or vegetables, but sometimes they get nothing, he said.

The prevalence of forced labour in Libyan migrant detention centres has been confirmed by other sources, including Human Rights Watch and the UN.

A report by the UN human rights office, released in December, said investigators had documented testimonies of forced labour in six detention centres between 2017 and 2018, including Tajoura and Triq al Sikka, which lies further north in Tripoli.

In October last year, a 28-year-old Somali man set himself alight in Triq al Sikka after dousing himself with petrol from a generator he accessed while working, according to witnesses. Abdulaziz felt hopeless about his chances of evacuation, friends said. The International Organization for Migration and the UN refugee agency confirmed the death.

A few months ago, another refugee described being made to work in a weapons store. “One day I have gone to work there. They have different weapons such as tanks with their rockets, and different bombs.”