Mathematician Alan Turing, who helped Britain win World War Two by cracking Nazi Germany's "unbreakable" Enigma code, was granted a rare royal pardon on Tuesday for a criminal conviction for homosexuality that led to his suicide.

Turing's electromechanical machine, a forerunner of modern computers, unravelled the code used by German U-boats in the Atlantic. His work at Bletchley Park, Britain's wartime codebreaking centre, was credited with shortening the war.

However, he was stripped of his job and chemically castrated with injections of female hormones after being convicted of gross indecency in 1952 for having sex with a man. Homosexual sex was illegal in Britain until 1967.

Get Breaking News Delivered to Your Inbox

Turing killed himself in 1954, aged 41, with cyanide.

Justice Minister Chris Grayling said the pardon from Queen Elizabeth would come into effect immediately and was a fitting tribute to "an exceptional man with a brilliant mind".

"His brilliance was put into practice at Bletchley Park during the Second World War where he was pivotal to breaking the 'Enigma' code, helping to end the war and save thousands of lives," Grayling said in a statement.

"His later life was overshadowed by his conviction for homosexual activity, a sentence we would now consider unjust and discriminatory and which has now been repealed," he said.

Only four royal pardons had been granted since the end of World War Two, a spokeswoman for Grayling said.

Cosmologist Stephen Hawking and 10 other eminent scientists had campaigned for years for "one of the most brilliant mathematicians of the modern era" to be pardoned.

One of those scientists, Paul Nurse, President of the Royal Society, said, "The persecution of this great British scientist over his sexuality was tragic and I'm delighted that we can now focus solely on celebrating his legacy."

In 2009, then Prime Minister Gordon Brown publicly apologised on behalf of the government for "the appalling way" Turing was treated but campaigners called for a full pardon.

In May 2012, a private member's bill was put before the House of Lords in the British parliament to grant Turing a statutory pardon and in July it gained government support.

Cameron on Tuesday described Turing as "a remarkable man who played a key role in saving this country in World War Two".

"His action saved countless lives. He also left a remarkable national legacy through his substantial scientific achievements, often being referred to as the father of modern computing," Cameron said in a statement.

The work at Bletchley Park, a secluded country house north of London, only became public knowledge in the 1970s when its role in the war and that played by Turing was revealed.

The cryptographers who worked there are credited with helping to shorten World War Two by up to two years and they deciphered around 3,000 German military messages a day.

Turing's team cracked the Enigma code, which the Germans regarded as unbreakable, as well as designing and developing Colossus, one of the first programmable computers.

But after the war, Prime Minister Winston Churchill ordered the Colossus computers and 200 "Turing bombe" machines be destroyed to keep them secret from the Soviet Union.