Image copyright EPA Image caption Polling stations were under tight security, and voting was relatively peaceful

Millions of Sri Lankans have voted in an unexpectedly close election that pits President Mahinda Rajapaksa against one of his former allies.

Mr Rajapaksa, in office since 2005, called the election two years early with analysts predicting an easy win.

But many voters have since rallied behind the challenger, former health minister Maithripala Sirisena.

There were armed policemen at every polling station because of concerns the vote would not be peaceful and free.

Mr Rajapaksa rode a wave of popularity after the civil war ended in 2009 but he now faces claims of cronyism.

His relatives fill many of the most powerful and influential positions in the country, and critics accuse him of running the state like a family business.

Analysts say Mr Sirisena appears to have capitalised on this perception, making gains among Sinhalese who usually vote for Mr Rajapaksa.

Mr Sirisena is also expected to win most of the vote from the ethnic minorities that make up about 30% of Sri Lanka's population.

Image copyright Reuters Image caption Maithripala Sirisena has surprised observers by generating huge support

Image copyright AFP Image caption But Mahinda Rajapaksa is still hugely popular, and defeat would be a big surprise

At the scene: Yogita Limaye, BBC News, Colombo

There has been a steady stream of voters at the polling booth I'm at in Colombo.

Azeez Hanifa, 50, came to vote with his wife and mother. He wants the end of corruption and what he calls "monarch rule".

His 74-year-old mother says she remembers the country in turbulent times and wants peace.

It's the first time undergraduate student Harintha Tillekaratne has voted in a presidential election. He wants more economic development and better quality education.

It's expected that the voter turnout will be high. So far, election day has been relatively peaceful.

Mr Rajapaksa, however, remains hugely popular with many in the Sinhalese majority.

Casting his ballot in his constituency on Thursday, the president said he was confident he would be given the leadership of the country for the third time.

"We will have a resounding victory. That is very clear. From tomorrow, we will start implementing our manifesto," he told reporters according to the AFP news agency.

He has presided over a period of impressive economic growth and still has political capital from being the leader who brought an end to the war.

Image copyright Getty Images Image caption The election is the second presidential poll since the end of the war

Image copyright AFP Image caption Many more voters in the north were expected to make it to polling stations than did in 2010

Sri Lanka is split along ethnic lines and Tamil rebels launched a campaign for a separate state in 1972.

The Rajapaksa government crushed the rebellion, but only after a bloody conflict had killed thousands of people.

Both sides in the 26-year civil war have been accused of atrocities.

The government has always denied allegations that many thousands of civilians were killed during the army's final assault on Tamil Tiger rebels.

The run-up to the current election - the second presidential poll since the war ended - was marred by violence and allegations of intimidation.

On Wednesday, an opposition activist died a day after being shot while organising a rally.

Voters in the Tamil-dominated northern city of Jaffna said there was a loud bang as polling stations opened, with some reports claiming a grenade had been thrown.

But observers said that voting had been broadly peaceful across the island.

Mr Rajapaksa was last elected in 2010 when he defeated his former army chief Sarath Fonseka, who was later jailed on charges of implicating the government in war crimes.

Polling stations opened at 07:00 (01:30 GMT) on Thursday, and results are expected on Friday.