The first “ghost comedy” was an effervescent 1937 charmer called Topper, in which two of the most elegant high comedians in movies, Cary Grant and Constance Bennett, crashed their roadster and immediately rebounded, their insouciant personalities utterly unchanged, as specters. That’s the joke on which ghost comedies are premised: death doesn’t alter a thing except corporeal reality.

The other convention of the genre is that though the ghosts have left the realm of the living, their link to the people they’ve left behind isn’t severed. Either they have unfinished business of their own to pursue or a new role to play in the lives of the living.

The entrancing Ghost Town, written by David Koepp and John Kamps and directed by Koepp, begins as a comic variation on M. Night Shyamalan’s The Sixth Sense. When Bertram Pincus (Ricky Gervais), a self-involved, antisocial dentist, undergoes a routine colonoscopy, the anesthesiologist screws up. Bertram is legally dead for seven minutes before the doctors manage to start his heart going again. Since he slept through the ordeal, he doesn’t suspect that anything out of the ordinary occurred until he notices that men and women on the street are following him around. They’re ghosts.

Once the ghosts realize that Bertram can see them (because he briefly crossed over the line between the living and the dead), they press themselves on him, demanding that he be an intermediary between them and the relatives, friends and co-workers they’ve left behind.

The most insistent of the ghost figures is Frank Herlihy (Greg Kinnear), whose elaborate demise opens the film. Though Frank wasn’t faithful to his wife, Gwen (Téa Leoni), when he was alive, he can’t stand the fact that since he died she’s become engaged again. Frank promises Bertram that if he can break up Gwen’s romance, he’ll see to it that every ghost who currently stalks him will leave him alone. Frank’s plan is for Bertram to court Gwen himself, which is quite a challenge considering that Gwen, who lives in the same apartment building as Bertram, thinks he’s a prize jerk.

Ghost Town veers into romantic-comedy territory as Bertram, with Frank’s assistance, manages to get Gwen to talk to him. The protagonists in a romantic comedy usually move from an adversarial relationship to an amorous one, proving themselves worthy of each other by their willingness to change. In Ghost Town only Bertram has to change—in this case, become human.

At first the movie displays the brusque, disdainful comic style familiar to viewers of Gervais’s BBC series The Office and Extras. For Bertram, the most conventional, uncomplicated everyday discourse is an intolerable ordeal; his refusal to extend himself in the most basic way to accommodate another person indicates a pathological need to distance himself. The reason for this behavior surfaces once Gwen gets him to explain that he moved to America out of love for a woman, now gone from his life. As a comic, Gervais is expert at conveying tiny psychological discomforts and awkwardnesses. You might not expect him to be as affecting as he is here, giving a performance dotted with small poignant moments.

Kinnear plays Frank as a debonair scamp; we can’t help being charmed by him even though the first thing we learn about him is that he can’t be trusted. One of the surprises of the movie is that Frank also experiences an epiphany and has to learn to look beyond himself, eventually learning that the unfinished business he’s still on earth to take care of has nothing to do with him. Both these actors are superb, and their disparate styles work together wonderfully. The linchpin of the movie, however, is Téa Leoni: if you can’t believe that Gwen can melt a man as bitter and isolated as Bertram, then Ghost Town doesn’t work. But it works brilliantly.

The movie has a lovely gleam (Fred Murphy shot it), a witty soundtrack (the Beatles’ “I’m Looking Through You” plays over the opening credits), and a way of touching you without ever going soft. Koepp and Kamps even find the ideal exchange between Bertram and Gwen to close the movie. Top to bottom, it’s a perfect romantic comedy.