Britain is closing its embassy in the embattled Libyan capital, Tripoli, and evacuating staff in tacit recognition that the country is now in a state of war.

The UK was, with Italy, one of the last embassies to remain in Libya in a week that has seen mass evacuations by diplomats, including the United States, France, Germany, Japan and the Netherlands.

With militia fighting worsening across the country and Libya's government having effectively collapsed, London appeared to feel it had no option but to withdraw its diplomats.

Ambassador Michael Aron tweeted: "Reluctantly we've decided we have to leave and temporarily suspend embassy operations in Libya. We will be back as soon as security allows."

He said the embassy staff would continue to function, operating from neighbouring Tunisia.

The Foreign Office said: "The British embassy is arranging an assisted departure for British nationals. Places are limited and requests for travel will be strictly prioritised. The British embassy in Tripoli will not be able to provide consular assistance after 4 August, after which date we plan temporarily to suspend operations."

Foreign Office officials were known to have become concerned about the level of fighting in Tripoli, where Islamist and nationalist militias have traded artillery fire for the past three weeks in fighting thought to have left several hundred dead.

Details of the withdrawal were not made public, but the pullout brings to a close Britain's role in trying to shape a post-revolutionary Libya, following its leading role in Nato's 2011 bombing campaign against the regime of Muammar Gaddafi.

London's diplomats have endured a bumpy ride: Aron's predecessor, Dominick Asquith, was ambushed in Benghazi, with two security guards wounded by rocket fire, in June 2012 – some three months before America's ambassador, Chris Stevens, was killed in the same city.

Last weekend, vehicles taking non-essential British diplomats out of the country were ambushed on Tripoli's outskirts, with the occupants of one vehicle saved only by bullet-proof glass that absorbed four high-velocity rounds.

In recent days, it has been considered too dangerous to work in the embassy itself, which is inside a city centre office block, leaving a skeleton staff at a fortified residence compound in the south-west of the city.

The compound had the affectionate nickname Ramsay Street, for its resemblance to the set of Australian soap opera Neighbours, but security officers changed that to the Alamo because of rockets sailing overhead as militias traded fire.

London had worked in concert with Paris and Washington to try to head off the present violence, but with both its partners gone, Britain appears to have decided little can be achieved by staying on.

• This article was amended on Saturday 2 August after the Foreign Office said the embassy had not yet closed.