LONDON — Nearly 60 years after his death, Alan Turing, the British scientist whose code-breaking work helped the Allies beat Hitler and whom many consider the father of artificial intelligence, received a royal pardon Tuesday for the crime of having had sex with another man.

Turing was convicted in 1952 of “gross indecency,” the charge used against gay men in an age when homosexual relations were illegal in Britain. He underwent chemical castration and had his government security clearance confiscated, then took his own life in 1954 at age 41, prematurely ending a distinguished career that pioneered today’s computer era.

In recent years, a campaign to have Turing’s name cleared has built momentum, resulting in an official apology in 2009 and culminating in the announcement Tuesday that Queen Elizabeth II, exercising her royal “prerogative of mercy,” had pardoned Turing at the request of the government.

The decision was hailed by many as long-overdue redress for one of Britain’s most brilliant scientists. But there was also criticism over the legal anomaly it created and the fact that tens of thousands of other men not fortunate enough to be as famous as Turing remain on the books as criminals for being gay.

“Dr. Turing deserves to be remembered and recognized for his fantastic contribution to the war effort and his legacy to science,” Justice Secretary Chris Grayling said. “A pardon from the queen is a fitting tribute to an exceptional man.”

Prime Minister David Cameron lauded Turing’s vital work in cracking the Nazis’ ingenious “Enigma” code, which had stumped some of the Allies’ best cryptographers. Deciphering the German military’s secret communications shortened World War II and “saved countless lives,” Cameron said.

Turing also is remembered for his path-breaking thinking on artificial intelligence and the idea that a machine could be programmed to perform multiple tasks. Long before the creation of modern computers, he developed the “Turing test,” an influential framework for determining whether a machine could be described as intelligent.

But his conviction for gross indecency shut down his career and subjected him to disgrace and appalling treatment. Forced to take female hormones to sap his sex drive, Turing was stripped of his clearance for government intelligence work and became bitter and depressed.

His death two years later from cyanide poisoning was ruled a suicide, though some of his friends and colleagues insisted it was an accident, and a few others muttered darkly of a plot by secret agents to kill him.

Turing’s story has been much-written about and dramatized for stage and screen. The play “Breaking the Code” won critical acclaim in the West End and on Broadway in the 1980s; a new musical based on Turing’s life and work, “The Universal Machine,” premiered in London this year. Shooting has begun on a film about Turing, “The Imitation Game,” starring Benedict Cumberbatch and Keira Knightley.

His posthumous pardon is highly unusual and possibly unique. Royal pardons normally are reserved for people who are innocent of the offenses they are accused of committing, and they usually are requested by family members or others close to the alleged offender. Neither is true in this case, a departure from protocol that reflects “the exceptional nature of Alan Turing’s achievements,” the government said.

Only subscribers are eligible to post comments. Please subscribe or to participate in the conversation. Here’s why.

Use the form below to reset your password. When you've submitted your account email, we will send an email with a reset code.