Libya's coastguard has seized two foreign flagged tankers and detained their crews for allegedly smuggling oil after an hours-long gun battle off the coast.

The coastguard spotted the vessels on Thursday night about 2km off Sidi Said near Zuwara, a town on the coast west of Tripoli, authorities said.

"The Libyan coastguard boarded the two tankers, one flying the Ukrainian flag, the Ruta, and the other, the Stark, flying the Congolese flag," said General Ayoub Qassem.

"The coastguards had monitored them from afar and waited until Friday morning to act," he told AFP news agency on Sunday.

Qassem said the oil traffickers were heavily armed and were supported by small boats.

They put up fierce resistance, but the tankers were eventually seized by Libyan authorities "after more than three hours of exchange of fire".

On several occasions, boats with armed men returned and opened fire with Kalashnikovs and heavy machineguns, he said.

READ MORE: Libya - Who controls what?

After they were overpowered, the two tankers and their crews were taken back to the capital, Tripoli, where they will face prosecution.

Among them were 14 Ukrainians from the Ruta and four Turks and two Georgians who were on the Stark, said Qassem.

Another three crew members from the Stark were in Zuwara, he said, without giving their nationalities.

At the time of the seizure, the Ruta had about 3,330 tonnes of oil in its tanks, while the Stark - which can carry 1,236 tonnes - was about to be loaded when the coastguard arrived.

READ MORE: The battle of Libya's key moments

Oil is Libya's main natural resource with reserves estimated at 48 billion barrels, the largest in Africa.

Libya had an output capacity of about 1.6 million barrels per day before the 2011 armed uprising, but production has since slumped as rival forces battle for control of its oil facilities.

Libya has been wracked by chaos since the 2011 uprising that toppled and killed longtime ruler Muammar Gaddafi, with dozens of armed factions emerging.

That uprising, which was initially unarmed, turned into a full-scale civil war, and NATO intervened on the side of anti-Gaddafi rebels.

Islamic State of Iraq and the Levant (ISIL, also known as ISIS) was able to capitalise on the country's instability and establish a foothold.