Rajapakse, a socialist who celebrated his 60th birthday on Friday, received 4.88 million votes, beating market-friendly Ranil Wickremesinghe who polled 4.69 million votes.
"The prime minister, under the election act... is duly elected as president of Sri Lanka," said elections commissioner Dayananda Dissanayake.
With Rajapakse securing 50.3% of the vote, analaysts say the results dimmed hopes for a peace process ending Sri Lanka's bloody civil war.
He is due to be sworn in at a ceremony on Saturday, taking over from incumbent President Chandrika Kumaratunga, after which he is expected to name a prime minister and a new cabinet.
News that the left-leaning Rajapakse had narrowly won saw the benchmark All Share Price index plunge 176 points, or 7% - the worst opening fall in two years.
It lost another three points during the day's trading to close at 2325.
Former air force chief Harry Gunatillake said the markets went into a spin because the victory of the hawkish Rajapakse meant that the peace process with the Tamil Tiger rebels could not be revived immediately.
"We won't see the new president in a position to start the peace talks soon because the nationalists who support him will not allow him to make any concession," Gunatillake said.
Rajapakse has promised a tough
line on dealing with Tamil rebels
He said Sri Lanka's peace negotiator Norway will also find it difficult to get used to the new dispensation in Colombo and the "getting to know" period would mean further delays.
Sri Lanka is under pressure from the international community to revive peace talks that have been suspended since April 2003, in order to qualify for billions of dollars in aid to rebuild tsunami-ravaged coastlines.
Two former Norwegian peace negotiators said on Friday that Rajapakse's victory could lead to deeper divisions and a rocky period in the country.
"The situation could be very difficult," Development Aid Minister Erik Solheim told Norwegian news agency NTB.
Another ex-mediator, former state secretary Vidar Helgesen, said Rajapaske's election could cement the divisions in the country.
Markets were first to react to what they saw as bad news.
"Everybody's dumping - there are net sellers in the market today following early election results," said Vajira Premawardhana, head of research at LOLC Securities.
Rajapakse is an outspoken opponent of privatisation and supports a leftist, state-dominated economy. Wickremesinghe campaigned for market reforms in a bid to woo more foreign investment.
Violence, meanwhile, continued to plague the country's east, with unidentified attackers lobbing grenades into a mosque in the town of Akkaraipattu during early Friday prayers.
The attack killed four men at prayer and wounded at least 25 others, police said.
Thursday's vote took place amid
Police said that during Thursday's vote, two Tamil Tiger rebels and a Sinhalese civilian died and 17 people were wounded in a string of seven bomb and grenade attacks in the east.
Private poll monitors said the Tamil Tiger rebels had effectively forced a boycott of the vote and prevented Tamils, who make up 12.65% of the population, from taking part.
There was virtually no voting in the embattled northern region while voting was low in the volatile eastern province, but the overall national average of polling was about 75%,
The vote chief made no reference to an appeal by Wickremesinghe's United National Party calling for a fresh poll on grounds that the blocking of voting by about 700,000 people in Jaffna may have robbed them of victory.
Fewer than 0.01% of the electorate turned out to vote in Jaffna after the rebel Liberation Tigers of Tamil Eelam said they were not interested in either of the two main candidates.
The rebels made no comment on the polling or the result.
The Tigers had branded the movie star-turned-prime minister the "war candidate" after he called for a full review of the Norwegian-backed peace process.