"These are people in the most vulnerable and dangerous of circumstances," U.N. High Commissioner for Refugees Filippo Grandi said in a statement, calling for them to be evacuated.
"They must be urgently brought to safety. Simply put, this is a matter of life or death."
Militia commander Khalifa Haftar has launched an offensive to take Tripoli from the U.N.-backed Government of National Accord (GNA), intensifying the crisis in the country riven by divisions since the NATO-backed overthrow of dictator Muammar Ghadafi in 2011.
Dozens of people have been killed in the past week and more than 300 have been wounded, according to the World Health Organization.
Since the conflict escalated last week, "more than 9,500 people have been forced to flee their homes," the UNHCR statement said.
Grandi underscored that migrants caught in the crossfire "are people in the most vulnerable and dangerous of circumstances. They have fled conflict or persecution in their own countries only to be trapped as conflict engulfs them again."
He pressed countries to respect legal obligations and support ways of ending the detentions, "including humanitarian corridors to evacuate those most vulnerable out of the country."
To date, only 150 such migrants have been relocated, from the Ain Zara detention center to an evacuation site, the U.N. body said.
Another 728 refused to leave the Qasr Bin Ghasheer facility unless they could be ferried out of Libya, it added.
Haftar launched his drive towards Tripoli on April 4, and is now engaged in a stand-off with a force loyal to the GNA lead by Faqez al-Sarraj.