Ghana's ruling party reclaimed a parliamentary seat from the main opposition on Tuesday in a closely contested by-election seen by both parties as a referendum on President John Mahama's rule ahead of a national election in 2016.

The ruling National Democratic Congress (NDC) will view its victory in the rural Talensi seat in Upper East region as evidence that it remains viable heading into the December 2016 election season after a series of setbacks.

Mahama's government won election in 2012 but is facing power shortages that cause prolonged electricity blackouts that have angered voters and hurt businesses.

It has also been forced into an aid program with the International Monetary Fund in an attempt to restore fiscal stability and jumpstart rapid GDP growth.

Growth has slowed sharply since 2014 due to a fall in global commodity prices for the country's exports of gold, oil and cocoa and a fiscal crisis viewed by many as the result of government mismanagement of the macro-economy.

In addition, severe flooding and a fire in June in the capital led to the deaths of 150 people.

The disaster was the worst in the West African country in decades and exposed infrastructure problems in what was until 2014 one of Africa's fastest-growing economies.

The next election, when Mahama is set to take on opposition leader Nana Akufo-Addo, will be a repeat of 2012, and analysts say it will be closely contested. Ghana has one of the most stable democracies in a region that experiences regular changes of power.

The NDC's Benson Tongo Baba won 42.3 percent of the vote in Talensi to beat the New Patriotic Party's Wuni Thomas Buanab who obtained 27.9 percent, the Electoral Commission said.

Six other contestants from smaller political parties won the rest. Turnout was 64.2 percent. The by-election will not change the balance of power in parliament where the ruling party now holds a majority of about 27 seats.

The seat became vacant after the opposition representative resigned to become a chief. Chiefs are an integral part of Ghana's system of authority, and the law bars them from engaging in partisan politics.

The by-election was peaceful, except for sporadic clashes between loyalists of the two rival parties that drew intervention by the police and army, poll observers said.