A British school teacher held hostage for nearly five months in Libya has been released, reportedly after a ransom was paid to his captors.

David Bolam, a 53-year-old head teacher was released from captivity in Benghazi, the UK Foreign Office confirmed late on Saturday.

"We are glad that David Bolam is safe and well after his ordeal, and that he has been reunited with his family," a Foreign Office spokeswoman said. "We have been supporting his family since he was taken."

British media said the headmaster was kidnapped in May and featured in an online video pleading for his life.

Bolam's captors, who described themselves as the 'Army of Islam', posted a video on September 7 of the teacher pleading for Britain to arrange a prisoner exchange or other diplomatic initiative to secure his release.

The BBC reported that his release was secured through the payment of a ransom, facilitated by local political factions.

The Foreign Office, which does not support the payment of ransoms, declined to comment on who held Bolam or how he had been released.

Libya is racked by violence as armed groups that helped topple Muammar Gaddafi in 2011 now battle each other and challenge for control of the country's vast oil resources and political domination.