In a statement issued later Tuesday, Libya said it had ordered the closure because Tripoli was not a signatory to the Geneva Convention on refugees and therefore did not recognise the UN agency's office in Tripoli.
"Libya, which is not a signatory of the Geneva Convention on the status of refugees... does not recognise the existence of a UNHCR office on its territory, and consequently, any activity undertaken by the office is illegal," said the statement issued by the Libyan foreign ministry.
It added that Libya had not signed a cooperation agreement with the refugee agency either.
In 2001, it had authorised the appointment of a UNHCR representative to Libya whose mission "was limited to solving a particular problem."
"But its work had become illegal thereafter," said the ministry, without specifying the problem.
The statement added that the question of "the illegality of the activities of the UNHCR office" and the need for its closure had been brought up several times with the UNDP office in Tripoli.
The North African state is a hub for refugees from sub-Saharan Africa as well as the Middle East seeking to reach Europe by crossing the Mediterranean Sea.
Last year, Italy and Libya reached an agreement that allows the Italian navy to intercept illegal migrants at sea and return them to Libya, triggering sharp criticism from the UNHCR and human rights groups.
"I think all European governments using Libya as a place where people fleeing from war and persecution could be received would have to review this carefully if UNHCR is no longer present there," Fleming commented.
The Council of Europe's anti torture watchdog warned in April that Italy was violating Europe's human rights convention by pushing boatloads of migrants back to Libyan shores, saying they faced the risk of maltreatment there.
The UNHCR currently provides registration, care, shelter, training and seeks resettlement elsewhere for asylum seekers and refugees who reach Libya, including Iraqis, Palestinians, Sudanese, Eritreans, Ethiopians and Somalis.
"Basically UNHCR is the asylum system in Libya and this will create a huge vacuum for the thousands of refugees and asylum seekers who are there already and of course those who continue to arrive... every week," Fleming warned.
The Geneva-based agency has registered about 9,000 refugees and some 3,700 asylum seekers in Libya, although there are thought to be more in the country.
The UNHCR also expressed concern that it took 24 hours to rescue a boatload of more than 20 mainly Eritreans, including an eight-year-old, in the Mediterranean.
Fleming said distress calls were received from the boat about 40 nautical miles (74 kilometres) from Italy late Sunday and passed on to Italian and Maltese authorities.
But they relied on Libyan vessels to conduct the rescue inside Malta's search and rescue area instead of intervening and taking the boat to a closer port, she claimed.
"This could have resulted in a dramatic human tragedy and we are asking why," said Fleming.
The refugee agency was also concerned about the Eritreans' "access to international protection in Libya," which has not signed the 1951 international convention on refugees "and has no domestic asylum system," she added.