More than 20 soldiers have been killed in a surprise attack in which fighters used speedboats in a failed bid to seize some of Libya's main oil terminals, according to officials.

Fighters launched Thursday's attack on al-Sidra port by firing rockets from speedboats, setting an oil tank on fire, security sources told AFP news agency.

Soldiers damaged three of the vessels before clashes in which the fighters were eventually repelled, sources said.

"These speedboats had fired several rockets at the terminals of Ras Lanuf and al-Sidra and one of them hit a tank south of al-Sidra port which then caught fire," said Ali al-Hassi, security spokesperson for the region.

Al-Sidra is in the "oil crescent" region that has been the scene of recent fighting between government forces and the Fajr Libya armed group.

Witnesses told AFP the attack was launched overnight, and reported seeing smoke from the burning oil tank.

Clashes were also reported from Sirte, a city west of al-Sidra.

While four soldiers died in al-Sidra, military and medical sources said 18 soldiers and a Fajr Libya fighter were killed in the clashes in Sirte, according to AFP.

Most of the dead soldiers belonged to the 136th battalion responsible for monitoring a power plant west of Sirte, the sources said.

Thursday's fighting in the oil-rich region came as pro-government forces lost ground to fighters in the eastern city of Benghazi, military officials said.

Libya has had two governments and parliaments since a group called Libya Dawn seized the capital in August by expelling a rival faction, installing its own prime minister and forcing the recognised prime minister, Abdullah al-Thinni, to operate out of the east with the elected House of Representatives.

The latest clashes pushed oil prices higher in Asia on Friday, with US benchmark West Texas Intermediate for February delivery rising 28 cents to $56.12, while Brent for February gained 13 cents to $60.37.