Second World War code-breaker Alan Turing has been given a posthumous royal pardon for a 61-year-old conviction for homosexual activity.

Dr Turing, who was pivotal in breaking the Enigma code, arguably shortening the Second World War by at least two years, was chemically castrated following his conviction in 1952.

His conviction for "gross indecency" led to the removal of his security clearance and meant he was no longer able to work for Government Communications Headquarters (GCHQ) where he had continued to work following service at Bletchley Park during the war.

Dr Turing, who died aged 41 in 1954 and is often described as the father of modern computing, has been granted a pardon under the Royal Prerogative of Mercy by the Queen following a request from Justice Secretary Chris Grayling.

"Dr Alan Turing was an exceptional man with a brilliant mind," Mr Grayling said.

"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.

"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.

"Dr Turing deserves to be remembered and recognised for his fantastic contribution to the war effort and his legacy to science. A pardon from the Queen is a fitting tribute to an exceptional man."

Dr Turing died of cyanide poisoning and an inquest recorded a verdict of suicide, although his mother and others maintained his death was accidental.

There has been a long campaign to clear the mathematician's name, including a well-supported e- petition and private member's bill, along with support from leading scientists such as Sir Stephen Hawking.

The pardon under the Royal Prerogative of Mercy will come into effect today.

The Justice Secretary has the power to ask the Queen to grant a pardon under the Royal Prerogative of Mercy, for civilians convicted in England and Wales.

A pardon is only normally granted when the person is innocent of the offence and where a request has been made by someone with a vested interest such as a family member. But on this occasion a pardon has been issued without either requirement being met.

In September 2009, then-prime minister Gordon Brown apologised to Dr Turing for prosecuting him as a homosexual after a petition calling for such a move.

An e-petiton - titled "Grant a pardon to Alan Turing" - received 37,404 signatures when it closed in November last year.

The request was declined by Lord McNally on the grounds that Dr Turing was properly convicted of what at the time was a criminal offence.

Prime Minister David Cameron said: "Alan Turing was a remarkable man who played a key role in saving this country in World War Two by cracking the German Enigma code.

"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."

Sadiq Khan MP, shadow justice secretary, said: "This is a long overdue recognition of the crucial role played by Alan Turing in the efforts to win World War Two and in our nation's proud record of scientific discovery.

"A pardon from the Queen is a truly fitting tribute and reflects the hard work of those who've campaigned for many years so that the shameful way he was treated could, in a small but important way, be righted."

Liberal Democrat peer Lord Sharkey, who introduced the private member's bill in the House of Lords calling for a royal pardon, said it was "wonderful" news.

Lord Sharkey said: "This has demonstrated wisdom and compassion. It has recognised a very great British hero and made some amends for the cruelty and injustice with which Turing was treated.

"I am proud to have been a part of the campaign. And I am proud of my colleagues in the House of Lords who strongly supported my Bill to grant a pardon to Turing.

"It's a wonderful thing, but we are not quite finished yet. I will continue to campaign for all those convicted as Turing was, simply for being gay, to have their convictions disregarded.

"That will be a proper and fitting and final end to the Turing story."

The Liberal Democrat MP for Manchester Withington, John Leech, described Dr Turing - who worked at the University of Manchester - as a hero.

He said: "Alan Turing's contribution to Manchester was enormous as well as his efforts in bringing the war to an early conclusion. He is a national hero.

"I want to thank all those who have campaigned so hard for this pardon. Without their efforts and their dedication, this would not have happened."