When a film opens with a title announcing "This really happened," then adds after a beat, "It really did," you know you're in for some kind of a rollercoaster ride. "I Love You Phillip Morris," based on events that trump anything you could dream up outside of a peyote ritual, is a delirious comedy that remains on track largely thanks to Jim Carrey's goofy but grounded performance.

As Stephen Russell, a gay Houdini who broke out of Texas prisons repeatedly to be with his beloved ex-cellmate, Carrey pushes the comedy to the limits without camping up his character. It's "Brokeback Mountain" starring the Three Stooges.

In the film's opening, Stephen narrates from a hospital bed as his vital signs drop. "Love sure is a funny thing," he muses, recalling days when he lived a serenely sanitized, bogus life. He was married (to the cheerfully oblivious Leslie Mann) with a couple of kids, played the organ at his hand-clapping church and pounded the beat as a cop. After a crisis jarringly reminds him you only go around once, Stephen rockets out of the closet ready to make up for lost time. He arrives in Miami determined to live in Liberace-level opulence. And if paying for it means staying afloat through phony insurance claims and credit-card scams, well, a fella's got to live, right?

Stephen is eventually nabbed and fellow convict Phillip, played by Ewan McGregor as an eyelash-batting Southern coquette, falls for him in a matter of minutes. Stephen's so glib and self-assured that he hardly seems to be a prisoner; the library where they meet could be his office.

Stephen, a master of bribery, courts his bunkmate with first-class chow and boombox oldies. When a night screamer frazzles Phillip's nerves, Stephen hires an inmate to thrash him into silence. Phillip mists up, declaring, "That is the most romantic thing anyone has ever done for me." In some of their scenes together, Carrey tamps down his ebullient mania and does a fair impression of a fraud unexpectedly touched by true love. Time and again he engineers wild schemes to reunite with his man. In the end it's his misguided attempts to live like royalty that strain the relationship; Phillip just wants to settle down into the kind of suburban routine that Stephen disdains.

Carrey and writer/directors Glenn Ficarra and John Requa keep us largely on his side. He's pathological, but his capers deliver a comedy-of-errors kick. Fans of "Liar, Liar" will appreciate the scenes where he impersonates a lawyer, and lies his way into a job as a corporate financial officer. He's a crook, but his narration half-convinces us that the people he embezzles from have it coming. They are soooo boring.

As they did in their script for 2003's "Bad Santa," Ficarra and Requa make a jaunty case for antisocial behavior. The story itself is a cleverly designed con job that echoes Stephen's swindles. "I Love You, Phillip Morris" delights in winning our sympathy, then revealing what dupes we've been. It's hard to hate a sociopath with such an affable grin.