Libya's Supreme Court has declared the election of Ahmed Maetig as prime minister to be unconstitutional.

In the latest political turmoil to strike the country, a judge at the Supreme Constitutional Court said on Monday "the election of Maetig at the General National Congress [was] unconstitutional".

Maetig, a 42-year-old independent backed by religious groups, was elected in a chaotic session of parliament in early May, in a vote that had been disputed by some politicians and judicial experts.

Al Jazeera's Stefanie Dekker, reporting from the capital Tripoli, said the GNC would comply with the court ruling and Abdullah al-Thinni would remain as caretaker prime minister.

The GNC will meet on Tuesday to decide on the next step.

Thinni had announced his resignation earlier this year after an armed attack on his family, but insisted his successor should be chosen by a new parliament rather than its contested predecessor and refused to recognise Maetig's cabinet.

Maetig was elected after clinching 121 votes, after a recount saw him pass the 120 votes needed under the constitution.

However, opposition politicians said the process suffered from irregularities after the first deputy adjourned the initial session.

Maetig's election prompted Thinni to refuse to hand power over until the judiciary decided on the matter.

Maetig's appointment adds to Libya's woes at a time when Khalifa Haftar, former general, is waging an offensive against Ansar al-Sharia fighters.

Haftar has said he will detain politicians he suspects of financing militias that he has blamed for Libya's strife.

The North African country has sunk into chaos following the downfall and death of Muammar Gaddafi in 2011.