Mousavi, who had himself declared victory just moments after the polls closed on Friday, described the decision to declare Ahmadinejad as the winner as "treason to the votes of the people".
"I personally strongly protest the many obvious violations and I'm warning I will not surrender to this dangerous charade," he said in a statement.
"The result of such performance by some officials will jeopardise the pillars of the Islamic Republic and will establish tyranny."
Mousavi complained that many people had been prevented from voting, in part due to a shortage of ballot papers, and said that the authorities had blocked text messaging, which his campaign has used to reach young voters.
Clashes broke out between supporters of the two rivals on the street of Tehran as the final results were announced.
Up to 3,000 Mousavi supporters then staged a sit-in in the middle of the road, clapping their hands and chanting: "Mousavi take back our vote! What happened to our vote?"
Witnesses said that police used batons to disperse the protesters, witnesses said.
Al Jazeera's Alireza Ronaghi, reporting from Tehran, said that there were group of young people walking the streets chanting slogans criticising Ahmadinejad.
"I saw a truck in which several people had apparently been arrested," he said.
The policy appears to be zero tolerance and no-nonsense."
Khameini urged supporters of the defeated candidates to avoid "provocative" behaviour or statements and back Ahmadinejad.
"The chosen and respected president is the president of all the Iranian nation and everyone, including yesterday's competitors, must unanimously support and help him," he said in a statement read out on state television.
Al Jazeera's Teymoor Nabili, reporting from Iran's interior ministry, said Mousavi is now likely to be thinking "very hard" about whether to take his complaints further.
"He has been told by the country's supreme leader that this is essentially the end of this election, and if he chooses to negate that command, he is laying down a challenge the like of which the Islamic Republic has reallly never seen before."
Trita Parsi, the president of National American Iranian Council, told Al Jazeera that the emphatic nature of the victory raised "a lot of question marks".
"There are so many inconsistencies. They are even reporting that Ahmadinejad won the city of Tabriz, which is Mousavi's home town, with 57 per cent. That seems extremely unlikely.
|Police were deployed to tackle protesters
in the Iranian capital [AFP]
"How come the votes were counted so quickly, even though the polls were open six hours extra?"
Ahmadinejad's supporters took to the street in the early hours of Saturday, waving Iranian flags and honking car horns, after the official Iranian Republic News Agency (IRNA) had declared the election for the incumbent president.
"Where are the greens? In a mousehole," some of them said, referring to the campaign colours of Mousavi, whose supporters held mass rallies in recent weeks.
The two other candidates up for election - Mohsen Rezai, a former commander of Iran's Revolutionary Guard, and Mehdi Karroubi, an ex-parliament speaker - finished a distant third and fourth.
Karroubi added his voice to those criticising the result, saying it was "illegitimate and unacceptable".