The race to become governor of Indonesia's capital was heading for a second round between the incumbent Christian and a Muslim former education minister after neither appeared to win a majority in a Wednesday election.
The Jakarta poll has been overshadowed by religious tensions, with Islamist-led protests against Governor Basuki Tjahaja Purnama, an ethnic Chinese Christian, and calls for voters to choose a Muslim leader for the city.
Analysts say divisions could linger and even deepen as the vote, which is also being widely seen as a proxy battle for the next presidential election, in 2019, appeared to be heading for a second round, according to unofficial sample vote counts.
Purnama is backed by President Joko Widodo's ruling party.
His main rival, former education minister Anies Baswedan, is backed by a retired general, Prabowo Subianto, who is promising a comeback to the national stage after losing to Widodo in the 2014 presidential vote.
"There would be tension that will be stored until 2019, because of course all this is not really against Purnama, it's against Widodo. Prabowo is coming in now," said Wimar Witoelar, a Jakarta-based political analyst.
Purnama had secured 43.08 percent of the votes, just ahead of Baswedan on 40.14 percent, based on a quick sample count of about 95 percent of the vote by private polling firm SMRC.
The other candidate, Agus Yudhoyono, the son of former President Susilo Bambang Yudhoyono, was in third place with 16.78 percent. Other pollsters showed similar results.
A candidate needs to get more than 50 percent of the votes in the first round to win outright.
The earliest a second round will be held is April.
The General Elections Commission is expected to announce official results in around two weeks.
Indonesia has the world's largest Muslim population but is officially secular and home to minority Christian, Hindu, Buddhist and other communities.
Overshadowing the campaign has been Purnama's trial for allegedly insulting the Koran in connection with remarks he made about how people vote.
He denies the charge.
The trial, which began in December, seemed to dent his support initially but more recently he has rebounded in opinion polls, helped by middle class approval of his efforts to improve the bureaucracy and tackle traffic jams and flooding.
Baswedan, who was dropped from Widodo's cabinet after a reshuffle in mid-2016, has largely stayed out of the headlines as the other two candidates - Purnama and Yudhoyono - fought a bitter campaign.
But Baswedan's strategy of targeting the Islamic vote, at a time when conservative Muslim groups were urging voters to shun a non-Muslim leader, gave him a late boost, analysts say.
"The votes may have shifted from Yudhoyono to Baswedan," said Irine Gayatri, a political analyst at the Indonesian Institute of Sciences.
Ethnicity and religion would likely again be major issues in a second round, Gayatri said.
The president said he hoped for an easing of tension.
"We hope that everybody can return as a family after these elections," Widodo said after casting his vote.
Purnama, dressed in his signature checkered shirt, met cheering supporters at his campaign headquarters.
"The struggle is not over," he told them. "Everyone wants just one round but we're grateful to have come at least this far."
Baswedan said his campaign for a second round would focus on policies.
"We will focus on programmes, about jobs, about quality education, needs that are important and urgent for families and people in Jakarta," he told reporters.
The votes in Jakarta and scores of other regions in the world's third-largest democracy were peaceful and mostly running without hitches, police said.
The Jakarta poll has been overshadowed by religious tensions, with mass Islamist-led protests against Governor Basuki Tjahaja Purnama, an ethnic Chinese Christian.
The vote is also being widely seen as a proxy battle for the 2019 presidential election.
Purnama had secured 42.57 percent of the votes, just ahead of former minister Anies Baswedan in second place with 40.23 percent, based on a quick sample count of around 40 percent of the vote by private polling firm SMRC.
The other candidate, Agus Yudhoyono, was in third place with 17 percent.
Other pollsters showed similar results. A candidate needs to get more than 50 percent of the votes in the first round to win outright.
The job of governor can be a springboard to the presidency and weeks of campaigning have been overshadowed by mudslinging, political intrigue and rising hardline Islamist sentiment, raising questions about the role of religion in politics.
"We hope that everybody can return as a family after these elections," President Joko Widodo said after voting in Jakarta earlier on Wednesday.
Purnama was a deputy to Widodo when he was the previous Jakarta governor and Widodo's party is backing him.
Purnama has been campaigning while on trial on a charge of insulting the Koran, a case that has brought Muslims onto the streets, urging voters to shun a non-Muslim as leader.
He denies the charge and after dipping in opinion polls his support rebounded, which analysts attribute to his record of improving the bureaucracy and easing congestion and flooding in Jakarta.
Indonesia has the world's largest Muslim population but is officially secular and home to minority Christian and Hindu communities, as well as hundreds of ethnic groups.
Baswedan is backed by a former general who Widodo beat in the last presidential election in 2014, Prabowo Subianto, who is promising a comeback to the national stage.
The elections in the capital, alongside scores of other regional polls in the world's third-largest democracy, were peaceful and mostly running without hitches, police said.
Police had deployed 75,000 personnel across Indonesia with 16,000 in Jakarta, concerned hardline Muslim groups may again take to the streets to oppose Purnama.