Image copyright AP Image caption KMT candidate Sean Lien (centre) bowed in defeat in Taipei

The Prime Minister of Taiwan, Jiang Yi-huah, has resigned after his ruling pro-China party suffered heavy defeats in local elections.

The Kuomintang party (KMT) appears to have lost control of districts across the country, including the mayor's office in the capital, Taipei.

Saturday's polls were widely seen as a referendum on relations with China.

KMT supporters had argued for good relations with China, which views Taiwan as a renegade province.

China and Taiwan, a close US ally, have been ruled separately since the end of a civil war in 1949.

Analysis: Cindy Sui, BBC News, Taipei

The defeat suffered by the KMT reflects voter dissatisfaction over the government's handling of food safety scandals, low wages and the widening wealth gap. It also reflects worries over its pro-China policy. Although trade deals with China have brought benefits to Taiwan, there is a sense that they have helped mostly companies, not ordinary people.

For them life has not got better since KMT President Ma Ying-jeou came into office in 2008. Instead, there are worries that closer ties with China will ultimately hurt Taiwan's independence - by making the island too economically dependent on its neighbour and vulnerable to pressures to reunify.

The KMT will now face an uphill battle in the 2016 presidential and legislative races. The question now is whether China will try to find ways to work with the DPP, a party it has long distrusted due to its support for Taiwan's independence. And will the DPP soften its stance ahead of the presidential vote?

Either way, a stalemate is likely in the short-term.

Bowing out

Mr Jiang told reporters he was stepping down in order to take responsibility for the defeat.

Moments later, President Ma Ying-jeou, who is also KMT chairman, apologised to supporters "for making everyone disappointed".

"I've received the message people have sent via these elections," President Ma told a news conference. "It's my responsibility and I will quickly offer a party reform plan to address everyone's demands. I won't avoid responsibility."

Image copyright AFP Image caption Ko Wen-je waves after his win in Taipei

The race in the capital was watched with particular attention as a test of Mr Ma's pro-China policy.

In the event, an independent opposition-backed candidate, Ko Wen-je, claimed victory over the KMT's Sean Lien.

Mr Lien publicly bowed in defeat along with his solemn-faced supporters. "We congratulate Mr Ko," he said in his concession speech. "I'm sorry I didn't win this election."

Lin Wen-chih, 48, a film producer who voted for Mr Ko, told AP: "We want to send the nationalists [KMT] a warning. Taiwan is an independent country. We don't want the nationalists to take measures that would have it eaten up [by China]."

More than 18 million eligible voters were registered to vote, choosing from among 20,000 candidates who were running for more than 11,000 positions.

Dependence fears

Some voters distrust the KMT, regardless of whether the deals signed with Beijing are good for Taiwan, the BBC's Cindy Sui in Taipei reports.

KMT supporters, on the other hand, feel that Taiwan needs good relations with its biggest trade partner to breathe new life into the island's ailing economy.

They feared a victory by the opposition Democratic Progressive Party (DPP) could cause relations with China and Taiwan's economy to regress, our correspondent says.

The DPP supports Taiwan's formal independence from China, something Beijing strongly opposes.

In 2016, Taiwan will hold the more important presidential and legislative polls.

Taiwan began allowing truly democratic elections - with opposition party candidates and universal suffrage - in the late 1980s.