"I hope first of all that asylum seekers and refugees can be taken out of detention centres," UN High Commissioner for Refugees Filippo Grandi said after visiting one centre in the Libyan capital.
"I fully appreciate that the government has security concerns," he said, but added that "other solutions" could be found for people fleeing countries in conflict such as Syria and Somalia.
Thousands of migrants are being held in dozens of detention centres in Libya, after being stopped or rescued from rickety boats as they attempted the perilous Mediterranean crossing to Europe.
"Children, women and men who have suffered so much already should not have to endure such hardship."
More From This Section
The UN refugee agency has secured the release of more than 800 refugees and asylum seekers from Libyan detention centres over the past year-and-a-half, the statement said.
Grandi promised to reinforce his agency's presence in Libya if security conditions allowed it, and also to provide assistance to thousands of Libyans displaced by conflict.
Libya descended into chaos after the 2011 uprising that killed and toppled Muammar Gaddafi, with rival authorities and militias vying for control of the oil-rich country.