A spokesman for the Ministry of Defence in London said: "After swift action by ISAF forces, which included UK personnel, the incident was contained." It is understood that the British troops were directly involved in the exchanges of fire.

A number of aircraft, hangars and other buildings at the base were hit and badly damaged by insurgent fire.

A Taliban spokesman said the attack was in revenge for an amateur film that mocked Islam, and because Prince Harry was known to be at the base.

The Prince, who was deployed recently, is not believed to have been involved in the fighting, which came after the Taliban fighters managed to break through an outer fence and attack the airfield inside the base.

The coalition base comprises the British Camp Bastion and the American Camp Leatherneck, and accommodates more than 21,000 people.

Camp Leatherneck itself, which was built to cope with the extra US troops, measures 13 sq miles. British troops are understood to provide security for the main runway on the American camp.

Rockets and mortar were fired from outside, damaging buildings and aircraft. As well as the deaths, a number have been reported wounded.

The forces said Bastion had not been breached.

A soldier from 1st Battalion Grenadier Guards was killed in Afghanistan yesterday after his vehicle hit a roadside bomb, unrelated to the attack at Camp Bastion in which two US Marines were killed, the MoD stressed.

The Guardsman died when his vehicle hit an improvised explosive device in the Nahr-e Saraj district of Helmand Province.

Earlier this year, an Afghan worker made an attempt on the life of US defence secretary Leon Panetta by driving at his plane as he landed at Camp Bastion.

The car burst into flames as it drove towards a welcoming committee, injuring a British serviceman.

Ghairat Baheer, a senior figure in Hizb-i-Islami, who has held three meetings with US officials in Kabul, said the withdrawal of US combat troops by the end of 2014 would leave Afghan forces ill-equipped to withstand the combined threat of insurgent groups.

However, earlier this week, Philip Hammond, the Defence secretary visited British troops in Afghanistan and revealed a sea change in the approach to the withdrawal taken by military commanders.

"I think there is a bit of a rethinking going on about how many troops we do actually need,” he said, speaking in an interview with the Guardian.

“There may be some scope for a little bit more flexibility on the way we draw down, and that is something commanders on the ground are looking at very actively."

Prince Harry is currently on duty as an Apache helicopter pilot in Camp Bastion after his deployment was revealed last week.

The Taliban dismissed his role as a propaganda stunt and claimed he was there to "atone" for the embarrassment caused by leaked pictures of him naked in a Las Vegas hotel room.

Zabiullah Mujahid, a spokesman for the insurgents, said: "The British Army is under pressure, just like the other forces in Afghanistan, and Britain wants to encourage its soldiers with such actions.

"And also the Prince was seen in naked pictures in England. To cover this shame, maybe he can atone by showing that he is fighting beside their soldiers in Afghanistan."

"The British haven't been able to do anything here in Afghanistan with all their soldiers. They can't do anything with their Prince here either," Mujahid added.

The brazen Taliban attack launched against the base is now the subject of a full security investigation. Capable of accommodating up to 20,000 troops, it was built in 2003 in a flat, isolated desert location that was chosen specifically for security reasons.

There are no towns or hamlets close by and soldiers on guard should have a clear view of anyone approaching from miles around.

The base is also ringed by a 30 foot concrete wall topped with double concertina barbed wire. Watchtowers are positioned every 100 yards inside, with an array of sensors designed to identify any movement beyond the perimeter. Bastion also has its own designated guard force who are responsible for security and regularly patrol the perimeter. While there have been insurgent attacks in the past, the Taliban have never been able to physically breach the camp walls.