The head of the UN refugee agency has urged Libyan authorities to free all asylum seekers and refugees from its detention centres.

Filippo Grandi, United Nations High Commissioner for Refugees, visited one of the detention centres in the Libyan capital of Tripoli on Sunday.

"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 refugees 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.

"I was shocked at the harsh conditions in which refugees and migrants are held," Grandi said in a statement released on Sunday by the agency's Geneva headquarters.

"Children, women and men who have suffered so much already should not have to endure such hardship."

UN needs to do more for refugees in Libya

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.

"The time has come for us, as the UN refugee agency, to step up our presence and activities in the country," Grandi later told AFP news agency.

He said that doing so could take time for political and security reasons, however.

"But meanwhile we are doing whatever we can to help the Libyans manage better these issues," he said.