Two British servicemen were disciplined and fined today for carrying out, respectively, a sexual offence on a young boy and being engaged in racist insults while serving in Afghanistan.

A non-commissioned officer was demoted in rank after being involved in photographing a civilian with a racist sign around his neck. His fellow defendant, a private who has since left the Army was fined a thousand pounds after pulling a boy's hand towards his crotch while saying "touch my special place".

At a court martial in Sennelager, Germany, Judge Advocate Large refused to lift an anonymity order on the two guilty men, citing the case of Gunner Lee Rigby who was killed outside military barracks at Woolwich. He said : "Very especially in the light of recent events in London and the threat posed by lone wolves it seems to me that it would be wrong to lift the restrictions. I accept that it would usually be wrong to make such an order and this should not be seen as an attempt by the military to hide behind the law. It was made in response to very specific circumstances and on specific grounds."

Download the new Independent Premium app Sharing the full story, not just the headlines

However, there have been increasing instances of anonymity being granted to military personnel charged with offences before the death of Gunner Rigby. In Autumn last year another judge, Advocate General, Jeff Blackett, refused a request for the identification of five Royal Marines charged with murdering an Afghan prisoner because, it was claimed, their lives would be put at immediate risk. Identities of defendants have been publicized in most previous cases, including that of soldiers tried over the killing of Iraqi detainee Baha Musa.

The two service personnel in today's hearing were referred to as Soldier X and Soldier Y. They had been serving with an infantry unit at the time of the offence, but neither were members of that particular regiment.

The court martial was shown video footage taken of Soldier X with a child of around five years of age, surrounded by other soldiers and laughing as he repeatedly asked him to touch him intimately.

He admitted insulting another Afghan child between October 16, 2011 and January 6, 2012. In a second piece of footage, which he shot himself, Soldier X can be heard making comments about other soldiers including the claim that they were paedophiles. When a boy around 10 years old comes up, smiling and offering to shake his hand, the soldier calls him a f***** c*** and tells him to f*** off.

Soldier Y, now 23, was photographed with a racist sign alongside an Afghan man. The court was shown a picture of him posing with an Afghan man holding the racist sign. Izzy Hogg, for the defence, said in mitigation that her client was not racist and had not been brought up in such a way. "His godfather is black and his best friend is Filipino" she said by way of explanation.

Judge Advocate Large deliberated together with a panel of three serving officers for over an hour.

Sentencing both men, he said the offences had taken place during a "demanding operational situation with risk of attack from insurgents and a persistent IED threat".

"Many soldiers develop their own strategies for dealing with the pressure of life on operations," he said.

"One of those strategies is banter, which has been a historic feature of military life and particularly life on operations.

"The boundary between what is acceptable and what trespasses into unacceptable behaviour is sometimes unclear and difficult to distinguish.

"Having said that, all soldiers, particularly those serving in Afghanistan, are fully trained and fully prepared for all aspects of your duties.

"We have heard that you were briefed on the sensibilities of the country and aware of the need to respect local customs and cultures and to avoid any behaviour that could be interpreted as having sexual or racist connotations which could cause understandable offence to people in that country."