Mathematician lost his job and was given experimental 'chemical castration' after being convicted for homosexual activity in 1952

Alan Turing, the second world war codebreaker who took his own life after undergoing chemical castration following a conviction for homosexual activity, has been granted a posthumous royal pardon 59 years after his death.

The brilliant mathematician, who played a major role in breaking the Enigma code – which arguably shortened the war by at least two years – has been granted a pardon under the Royal Prerogative of Mercy by the Queen, following a request from the justice secretary, Chris Grayling.

Turing was considered to be the father of modern computer science and was most famous for his work in helping to create the "bombe" that cracked messages enciphered with the German Enigma machines. He was convicted of gross indecency in 1952 after admitting a sexual relationship with a man.

He was given experimental chemical castration as a "treatment". His criminal record resulted in the loss of his security clearance and meant he was no longer able to work for Government Communications Headquarters (GCHQ), where he had been employed following service at Bletchley Park during the war. He died of cyanide poisoning in 1954, aged 41.

Announcing the pardon, Grayling said: "Dr Alan Turing was 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.

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

David Cameron described Turing as a "remarkable man". The prime minister added: "His actions 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."

There had been a long campaign to clear Turing's name, including a private member's bill. In 2009, an "unequivocal apology" was issued by then prime minister Gordon Brown. An e-petition calling for a pardon received 37,404 signatures when it was closed in November last year. The request was declined by the then justice minister Lord McNally on the grounds that Turing was properly convicted of what at the time was a criminal offence.

A pardon is normally granted only 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. On this occasion, a pardon has been issued without either requirement being met.

There was mixed reaction to the announcement. Iain Standen, chief executive of the Bletchley Park Trust, said Turing was "a visionary mathematician and genius whose work contributed enormously both to the outcome of the war and the computer age".

He added: "The pardon gives further recognition for his outstanding contribution not only to second world war codebreaking but also the development of computing."

Dr Andrew Hodges, tutorial fellow in mathematics at Wadham College, Oxford, and author of the acclaimed biography Alan Turing: The Enigma, said: "Alan Turing suffered appalling treatment 60 years ago and there has been a very well intended and deeply felt campaign to remedy it in some way. Unfortunately, I cannot feel that such a 'pardon' embodies any good legal principle. If anything, it suggests that a sufficiently valuable individual should be above the law which applies to everyone else.

"It's far more important that in the 30 years since I brought the story to public attention, LGBT rights movements have succeeded with a complete change in the law – for all. So, for me, this symbolic action adds nothing.

"A more substantial action would be the release of files on Turing's secret work for GCHQ in the cold war. Loss of security clearance, state distrust and surveillance may have been crucial factors in the two years leading up to his death in 1954."

Writer David Leavitt, professor of English at Florida University and author of The Man Who Knew Too Much: Alan Turing and the Invention of the Computer (2006), said it was "great news". The conviction had had "a profound and devastating" effect on Turing, Leavitt said, as the mathematician felt he was being "followed and hounded" by the police "because he was considered a security risk".

"There was this paranoid idea in 1950s England of the homosexual traitor, that he would be seduced by a Russian agent and go over to the other side," Leavitt said. "It was such a misjudgment of Alan Turing because he was so honest, and was so patriotic."

With the situation in Russia regarding LGBT rights, and the recent decision by the supreme court in India to reinstate the criminalisation of homosexuality, "for this to come from the Queen, is going to send a really important message, especially to the Commonwealth", Leavitt added.Prof S Barry Cooper, professor of mathematical logic at the University of Leeds and chair of the Turing Centenary Advisory Committee, said: "There was a slight worry that the private member's bill had got slightly bogged down. So this is fantastic. And a sign of the growing reputation of Alan Turing, a growing sense of what he did for this country and what so many other people at Bletchley Park did."

Liberal Democrat peer Lord Sharkey, who introduced the private member's bill in the House of Lords, 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.

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

Gay rights campaigner Peter Tatchell said the royal pardon was long overdue, but also due to "another 50,000-plus men who were also convicted of consenting, victimless homosexual relationships during the 20th century".

Though an inquest recorded a verdict of suicide, Turing's mother and others maintained his death was accidental.

Turing made highly significant contributions to the emerging field of artificial intelligence and computing. After the war, he worked at many institutions, including the University of Manchester, where he worked on the Manchester Mark 1, one of the first recognisable modern computers.