British media agreed not to report the prince's deployment
The 23-year-old royal, who has spent the last 10 weeks serving in Helmand Province, is flying back to the UK amid concerns for his safety.
The move follows the collapse of a news blackout deal over his tour of duty, which was broken by foreign media.
There had been fears the prince, who is third in line to the throne, could become a target for the Taleban.
In a statement, the Ministry of Defence described the reporting of Harry's deployment by foreign media as "regrettable" but said that contingency plans for such a leak were in place.
Prince Harry, in an interview recorded in Afghanistan prior to his withdrawal, said he had enjoyed being away from the press and England.
"I don't want to sit around Windsor, because I generally don't like England that much and it's nice to be away from all the press and the papers.....," he said.
More recently he took part in a major operation to disrupt Taleban lines of communication
Brigadier Andrew Mackay
It added that while the prince should have returned "in a matter of weeks" with his Household Cavalry regiment battlegroup, the situation had now "clearly changed".
Brigadier Andrew Mackay, Commander of Task Force Helmand, said Harry had been "deployed in the field, conducting operations against the Taleban" at the time of the decision.
He continued: "He has seen service both in the south of Helmand and in the north. More recently he took part in a major operation to disrupt Taleban lines of communication."
'Risks'
Chief of the Defence Staff, Air Chief Marshal Sir Jock Stirrup, in consultation with head of the Army, General Sir Richard Dannatt, had taken the final decision to withdraw Harry immediately, the statement said.
"This decision has been taken primarily on the basis that the worldwide media coverage of Prince Harry in Afghanistan could impact on the security of those who are deployed there, as well as the risks to him as an individual soldier," it added.
Prime Minister Gordon Brown paid tribute to the prince and said Britain owed him a "debt of gratitude" for his service in Afghanistan, but he added that it was correct to bring Harry back to the UK.
HAVE YOUR SAY Prince Harry is a trained soldier. I'm in the service too and I'm pleased for him that he was allowed to go Deborah, Hampshire, UK
"Security considerations come first. That has been the deciding factor which was made by our defence staff and I think that everybody will respect that is the right decision."
He thanked Harry, a second lieutenant, for the "professionalism and dedication he has shown", and said the decision to bring him home was a reminder of the "difficulties and challenges" the armed forces faced on active duty.
Conservative leader David Cameron agreed that it was "right" to withdraw the prince from Afghanistan, but said everyone in Britain should be "proud of what he has done".
"It's incredibly tough out there. He's obviously shown great courage and bravery as all our soldiers do out there.
"And what they do is really important, not just for the future of Afghanistan but for the safety of our country too."
'Dangerous tasks'
A member of the Household Cavalry, Prince Harry was based in a former madrassa along with a Gurkha regiment.
Work involved calling up allied air cover in support of ground forces and going out on foot patrols.
Defence Secretary Des Browne also commended Harry, saying the prince was "an example of a generation of young people" who were "prepared to take on these very serious and dangerous tasks for our security".
The Queen, opening the Queen's Court Care Home in Windsor, said she believed he had done "a good job in a very difficult climate".
THE EDITORS' BLOG A news black-out is unusual, but not unique
Jon Williams,
World news editor, BBC News
Jon's comments in full
The prince's deployment was subject to a news blackout deal struck between the MoD and newspapers and broadcasters in the UK and abroad.
It is understood that the news was first leaked in an Australian publication in January but only after it appeared on the influential US website, The Drudge Report, did the deal break down.
In exchange for not reporting the prince's deployment, some media organisations were granted access to the prince in Afghanistan for interviews and filming.
The prince's withdrawal is the second major blow to his army career.
Last year, a planned tour to Iraq had to be cancelled at the last minute because of a security risk.
Prince Henry of Wales
Prince Harry has today been ordered to return home, after details that the 23 year old's front line engagement was leaked by an American website.  The details of the Prince's engagement had previously been kept secret by many British and international news agencies, including the BBC and the Associated Press.

The leak of the information was said to be "regrettable" by the Ministry of Defence, however this meant that Prince Harry was ordered to return home due to fears for his, and the others in his unit's safety.  Prince Harry will be flown home "immediately", after 10 weeks of front line service.  During this he worked as a Joint terminal attack controller (JTAC) and was involved in major operations on the frontline against Taliban forces.

The media blackout was created to lower the risk to the royal, whilst in service in Afghanistan, and was the first large scale agreement of its kind.  The media groups who signed up to the agreement were rewarded with interviews with the Prince.