* Morsy 51.73 pct vs Shafik 48.27; margin 882,751 votes
* Islamist beats ex-general who was Mubarak's last premier
* Cheers from Muslim Brotherhood in Tahrir Square
* Army to retain much control for time being
* Brotherhood says struggle for control just starting
* Morsy says to be president for all Egyptians (Adds Morsy remarks, further reaction)
By Yasmine Saleh and Marwa Awad
CAIRO, June 24 (Reuters) - Islamist Mohamed Morsy was declared Egypt's first freely elected president on Sunday, sparking joy among his Muslim Brotherhood supporters on the streets who vowed to wrest more power from armed forces reluctant to cede ultimate control.
But many Egyptians, and anxious Western allies, urged Morsy to work fast to repair a country economically stricken and bitterly divided since the overthrow of Hosni Mubarak fractured the police state which had suppressed dissent for decades.
Morsy defeated former general Ahmed Shafik in a run-off last weekend by a convincing 3.5 percentage points, or nearly 900,000 votes, taking 51.7 percent of the total, officials said.
The announcement ended a week of angry disputes over the count in the poll to replace Mubarak, pushed aside by his fellow officers 16 months ago to appease the Arab Spring revolution.
In his first address to the people, many of whom voiced fears of Islamic law and a threat to secular traditions, Morsy insisted: "I am today a president to all Egyptians.
"There is no room now for the language of confrontation," he said. "This national unity is now the route Egypt must take to move itself beyond this difficult period."
The military council which has ruled the biggest Arab nation since February 11, 2011, curbed the powers of the presidency by decree last week, meaning the head of state will have to work closely with the army on a planned democratic constitution.
The generals say they want to hand over to civilian rule but are plainly set on defending their privileges and suspicious of the ability of Egypt's fragmented, and long oppressed, political movements to establish a stable constitutional democracy.
Calling it a "milestone in their transition to democracy", the United States, the army's key sponsor and also long wary of the rise of political Islam, joined other Western powers in congratulating Morsy and calling on him to form a government of national unity that would respect all Egyptians' civil rights.
BROTHERHOOD WANTS MORE
Brotherhood officials, speaking as supporters turned Cairo's Tahrir Square into a roaring sea of flags and chants of "Allahu akbar!" (God is greatest), said they would press on with protest vigils to demand that the ruling military council cancel this month's dissolution of the Islamist-led parliament and a decree which gave the generals powers that will restrict the president.
"Morsy is the first truly democratically elected president in Egypt," the Brotherhood's Yasser Ali told Reuters. "He has the legitimacy and will sit down with the military council and all the political forces to resolve the outstanding issues."
"Speak! Have no fear! The military must go!" crowds chanted on Tahrir Square, seat of the revolt. But like Morsy, other officials in a movement that opposed Egypt's army for six decades and was the inspiration for Islamist parties across the region, said they wanted to avoid outright confrontation.
There were some isolated scuffles in parts of Cairo between rival groups. Several hundred Shafik supporters in the middle-class suburb of Nasr City chanted "Save Egypt! The Brotherhood will destroy it!", while soldiers tried to keep traffic moving.
Field Marshal Hussein Tantawi, who heads the ruling Supreme Council of the Armed Forces (SCAF), called to congratulate the 60-year-old Morsy on his victory, state television said.
How these two men cooperate will determine Egypt's uncertain path from revolution to democracy and its relations with wary Arab and Western allies: Tantawi was Mubarak's defence minister for 20 years and has been close to the Pentagon; Morsy, jailed more than once under the old regime, has a doctorate in engineering from the University of Southern California.
"We thank God for reaching this historic moment, this luminous moment which Egyptians achieved by their own hands, their blood, their tears and their sacrifices," Morsy said, calling his election a "victory of the national will".
DISMAY FOR DIEHARDS
Those who voted for Shafik as a bulwark against a religious rule that they fear will mean intolerance and alienation from the West were fearful: businessman Maged Abdel Wadud, 45, who had gathered with others at a hotel hoping to greet a victorious Shafik said: "This is a very bad day for Egypt.
"I am so, so upset. I can't imagine this man becoming a president of Egypt. This is the beginning of the end for Egypt."
Some of those backing Shafik accused the military council and Tantawi of "selling out" to the Brotherhood. But a source close to the council itself insisted the election had been entirely fair, in contrast to those under Mubarak.
Describing it as "a true example of democracy to the world" the military council source stressed the pressure was now on Morsy to take responsibility - something many think might backfire on the Brotherhood if Egypt remains in crisis:
"The onus now is on the new president to unite the nation and create a true coalition of political and revolutionary forces to rebuild the country economically and politically," the source said. "The world is now watching the new president."
Western powers, and Israel, have been concerned about the Islamist turn in Egypt. But Washington and Europe, both big aid donors, have also pressed the military to accept democracy, while urging the Brotherhood to respect all Egyptians' rights - notably those of women and the large Christian minority.
A White House spokesman, naming women and Christians as important interest groups said: "It is important for President-elect Morsy to take steps at this historic time to advance national unity by reaching out to all parties and constituencies in consultations about the formation of a new government."
The British and French governments echoed that sentiment.
In Israel, at war with the Brotherhood's Palestinian offshoot Hamas, Prime Minister Benjamin Netanyahu said he "respected" the democratic outcome in Egypt and expected to continue cooperation under the two states' peace treaty.
Morsy, in a televised statement behind a presidential crest, said he would honour all international treaties, while not mentioning the Jewish state by name in a speech punctuated by religious language never used by his secular predecessor.
In Hamas-run Gaza, where Palestinians hope Morsy may end Cairo's cooperation with an Israeli blockade, celebratory gunfire rattled across the crowded coastal territory.
Iran, which is at odds with most Arab states, welcomed what it called an "Islamic awakening" in Egypt.
A senior Western diplomat in Cairo called it "a triumph over the politics of fear and prejudice" and said: "The Muslim Brotherhood are far from a perfect organisation, but Morsy's election represents a genuine result for the revolution."
He said he did not expect the movement to push its complaints so far as to provoke the military to hit back hard: "The Muslim Brotherhood will take what they've got - a prize unimaginable to them 18 months ago," the diplomat said. "An imperfect presidency is way better than none at all.
"It's part of the new and delicate act of political compromise, part of Egypt's new cohabitation."
LIBERALS CAUTIOUS, CHRISTIANS GLOOMY
Half of those who voted in last month's first round of the election backed neither Morsy nor Shafik and many who voted in the run-off voted negatively - either against Morsy's religious agenda or against Shafik as a symbol of military rule.
"We voted for Morsy reluctantly to prevent Shafik from getting in because if he were president, we would all be hanged," said Mohamed Abdel Latif, 28, a youth activist in the second city of Alexandria. "From today, we will oppose Morsy.
"The Brotherhood must remember that they won because of us and they shouldn't repeat the mistakes of the past."
Hamdeen Sabahy, the secular leftist who finished a close third behind Shafik in the first round, called on Morsy to "swiftly form a national presidential administration and a government that expresses national reconciliation".
Shafik, a former air force commander and Mubarak's last prime minister, offered his congratulations and said he was willing to serve in Morsy's administration if asked.
He had the support of many in Egypt's 10-percent Coptic Christian minority, many of whom reacted gloomily to the outcome. Ehab Aziz of the Coptic American Friendship League said fellow Christians were worried they would face special taxes, as some interpretations of Islamic law suggest for non-believers.
Joe Fahim, 29, a Christian in Cairo, said he felt "apathetic", adding: "For most Copts though, it's doomsday."
MORSY TO STRUGGLE FOR CONTROL
Analysts warned that Morsy may struggle to bring Egyptians the stability and prosperity they yearn for after stagnation and corruption under Mubarak, followed by 16 months of crisis.
"President Morsy will struggle to control the levers of state," Elijah Zarwan, senior policy fellow at the European Council on Foreign Relations, said in Cairo.
"He will likely face foot-dragging and perhaps outright attempts to undermine his initiatives from key institutions. Faced with such resistance, frustration may tempt him to fall into the trap of attempting to throw his new weight around," Zarwan told Reuters. "This would be a mistake.
"His challenge is to lead a bitterly divided, fearful, and angry population toward a peaceful democratic outcome, without becoming a reviled scapegoat for continued military rule."
Morsy has promised an "Egyptian renaissance with an Islamic foundation". Yet the stocky, bespectacled party official appears something of an accidental president: he was only flung into the race at the last moment by the disqualification on a technicality of Khairat al-Shater, the group's preferred choice.
With a stiff and formal style, Morsy cast himself as a reluctant latecomer to the race, who cited religious fear of judgement day as one of his reasons for running. He struggled to shake off his label as the Brotherhood's "spare tyre".
Questions remain over the extent to which Morsy will operate independently of other Brotherhood leaders once in office: his manifesto was drawn up by the group's policymakers. The role Shater might play has been one focus of debate in Egypt.
Turnout was only 51.8 percent of the 50 million-strong electorate, slightly up on the first round but indicative still of a nation unused to having its voice heard without risking punishment and uncertain of the worth of the candidates. (Additional reporting by Shaimaa Fayed, Tamim Elyan, Tom Perry, Edmund Blair and Patrick Werr; Writing by Alastair Macdonald; editing by Philippa Fletcher)