Alan Turing, an ingenious British mathematician, cryptanalyst and, as presented in “The Imitation Game,” difficult personality, seems an unlikely hero. Yet the history-based Turing biopic convincingly portrays this conflicted bundle of brilliance and repression as a key player in the Allies’ World War II victory.

British actor Benedict Cumberbatch gives an empathy-stirring performance as Turing. A Cambridge fellow in 1939, the then-27-year-old Turing is recruited by Britain’s top secret Government Code and Cypher School at Bletchley Park. He joins a team that’s working to break the Nazis’ supposedly unbreakable Enigma code.

But Turing does not play well with others. He has no patience for those less gifted than himself. As the on-the-march Nazis conquer and destroy, Turing alienates the other would-be code breakers and isolates himself.

Cumberbatch puts depth and mystery into a haunting portrayal of Turing, a misanthropic fellow who is most happy when he’s tinkering with his under-construction code-breaking machine. He names the contraption Christopher.

The other Bletchley Park team members include their leader, a dapper chess champ and lady’s man; a Scottish mathematician who’s not in Turing’s league; an Oxford undergraduate; and two linguists.

The team quickly resents the unapologetically aloof Turing. The single-minded cryptanalyst does nothing to change their collective opinion.

Mark Strong (“Tinker Tailor Soldier Spy,” “Sherlock Holmes”), who usually plays villains, co-stars as the calculating MI6 leader Stewart Menzies. A stereotype-defying mix of hero and villain, Menzies supports Turing behind the scenes. But Menzies will always place his own interests above anyone else’s.

Turing, on order of British Prime Minister Winston Churchill, is named leader of the Bletchley Park code breakers. His new authority further antagonizes the others. Turing’s unconventional addition of a woman to the group helps soothe the all-male team’s outrage.

Keira Knightley’s Joan Clarke, a young woman who’s even better at crossword puzzles than Turing, brings her own genius to Bletchley Park. Also ideas that may help Turing and his warring teammates unite for the higher purpose of breaking the Enigma code. The group’s eventually cooperative quest grows suspenseful and exciting.

In Clarke, Turing also finds something the film strongly suggests he’s rarely had — a friend. But their friendship also is the source of one of the film’s most tragic scenes.

Like most recent biopics, “The Imitation Game” reveals its subject in both the present and flashbacks to much younger years. The Turing biopic does that better than most.

In 1927, the 15-year-old Turing (Alex Lawther) is a persecuted misfit at Sherborne School, Dorset. Despite the largely hostile environment, Turing develops a warm relationship with fellow student Christopher Morcom (Jack Bannon). Morcom’s influence helps shape Turing’s future, globally significant accomplishments.

A film set in three time periods, “The Imitation Game” also details Turing’s postwar downfall. In early 1950s Britain, homosexuality is a crime. Turing’s fate is unsettling, especially after he helped defeat his nation’s enemies. Cumberbatch’s haunting performance as Turing ensures that his tragedy is keenly felt.

In the tradition of “The King’s Speech” — which shares filmmaking talent with “The Imitation Game” — the Turing biopic is awards bait. Cumberbatch is so compelling, though, and Turing’s story, unknown for decades, is told so well by director Morten Tyldum and screenwriter Graham Moore, that even obvious awards-casting moments can be forgiven. “The Imitation Game” is easily among 2014’s best films.