The first part of “The Escape Artist,” airing tonight on “Masterpiece: Mystery,” is a bit of a misdirection.

Written by David Wolstencroft (“Spooks”), this thriller begins as a legal drama and slowly turns into being about something much more primal.

It stars the always interesting David Tennant, who will be seen in this fall’s “Gracepoint” on Fox. He plays slick defense lawyer Will Burton, whose clever tactics can get even the most obvious guilty off.

Everyone deserves a defense, he reasons. For this he is rewarded with a life is close to a fairy tale, with a beautiful wife (Ashley Jensen), and a son, a posh home in London, a cottage in the country and the adoration of most of his peers — at least those not envious of him.

One of those would be Maggie Gardner (Sophie Okonedo from “Hotel Rwanda”), who has spent much of her life finishing second to him, either in school or in the courtroom.

Then Will is given the case of Liam Foyle (Toby Kebbell), accused of the brutal torture and killing of a young woman.

There is clearly something off about the man. The apartment full of birds and his admittedly antisocial feelings are easy to spot, but Kebbell gives Liam an air of suppressed menace, like a predator sizing up his prey.

“Have you seen the pictures?” Will asks a colleague about photos of the victim. “She was alive for most of it.”

Despite his obvious distaste for Liam, the attorney stays the course — this is what he gets pays for, after all — and finds a way to have the case thrown out over a point involving whether Liam had visited porn websites that specialized in torture.

Afterward, Liam, who has a smugness to him, tries to shake Will’s hand to thank him, but the lawyer, who had previously exhibited his own smugness, draws the line at touching such an obvious degenerate and walks away. This is where the story takes off.

What follows are some wild twists and turns — a few fairly grisly, though most are not shown.

Tennant is marvelous at negotiating these, with Will suddenly finding himself at the wrong end of some kind of weird karmic justice.

Okonedo is quite good as Maggie, who lets ambition and jealousy take over while trying to tamp down her conscience. Kebbell is simply chilling.

The dark “The Escape Artist” occasionally stretches plot points, but it always quickly snaps back and keeps you guessing until the end.