Taiwan's close-fought election has ended in high drama, a day after President Chen Shui-bian survived an apparent assassination attempt.
Lien Chan is demanding a recount
The Election Commission has declared that he has been re-elected to a second term by fewer than 30,000 votes.
But challenger Lien Chan said questions about the shooting of Mr Chen made the election unfair, and he demanded that the results be annulled.
A referendum on relations with China failed to muster enough support.
Chen Shui-bian: 6,471,970 (50.11%)
Lien Chan: 6,442,452 (49.89%)
337,297 invalid ballots
Source: Central Election Commission
Participation did not top the required 50% threshold and will be seen as a significant defeat for Mr Chen, who had said a failure of the historic referendum would be a victory for China, which had opposed the vote.
Mr Chen had sought support for proposals that Taiwan should seek peaceful resolution of differences with Beijing while upgrading missile defence systems.
He called on China to accept his re-election and to remove the missiles aimed at the island Beijing considers a renegade province.
Although China has not directly commented on the election result, it condemned the referendum.
It said the outcome showed the proposals went against popular opinion and were doomed to failure.
'Clouds of suspicion'
Official results show that Mr Chen's margin of victory over Mr Lien was less than 0.25%, with more than 13 million ballots cast.
"This slim gap has been achieved under clouds of suspicion," Mr Lien told a sombre crowd of supporters.
Pre-election opinion polls had suggested the president was trailing Mr Lien of the former ruling nationalist Kuomintang party - although the race was tight.
Observers say Mr Chen may have picked up some sympathy votes after he and his running-mate, Vice-President Annette Lu, were shot while campaigning in the southern city of Tainan.
Both were allowed home after receiving hospital treatment.
Mr Lien urged his supporters to stay calm and rational, but said there had still been no explanation of the shooting attack.
"It's not a fair election - prepare to apply to annul the election," he said - at which point the crowd began cheering and blowing horns in support.
Analysts told the BBC a petition has already been filed with a court in the southern city of Kaohsiung to examine the validity of the poll but Vice-President Lu rejected claims that it was unfair.
"If you make the petition without any evidence, then the democracy that we took pains to establish could be damaged," said Ms Lu - whose knee was hurt in Friday's shooting - before bursting into tears.
President Chen did not respond to the opposition's complaints, but appealed for tolerance and called for unity among all Taiwanese.
"This is not a personal or party victory, it's a victory for democracy and the people of Taiwan," he told jubilant supporters.
The BBC's Chris Hogg in Taipei says it is unclear what will happen next, though attention may focus on several hundred thousand votes declared invalid, which could influence any recount.
He adds that there are fears of possible political or physical conflict in a country so clearly divided.
Newspapers are full of speculation about who might have been responsible for the shots fired at President Chen and his deputy.
The president was shot in the stomach and Vice-President Annette Lu was hit in the knee as they campaigned in the leader's southern home town of Tainan.
Taiwan's national security bureau has ruled out involvement by China or illegal gambling rings, who are rumoured to have staked millions of dollars on the election outcome.
But the authorities will not comment on who they think is behind the attack. No-one has yet been arrested.