Comedy, it seems, is a lot like sex. There’s foreplay, in the form of a set-up, followed by a crescendo of excitement and release created by a mysterious combination of rhythmic repetition and an element of surprise that’s hard to define but easy to recognize when it works. Climax either comes — in the form of laughter — or it doesn’t.

By that measure, “Sex Tape,” about a married couple whose intimate home movie has been accidentally shared with the world, satisfies, albeit in a way that’s less than earth-moving. There are recurring gags — some of which work and some of which don’t — and one very nice surprise. Rob Lowe (who, as it happens, was once the star/victim of his own sex tape) nearly steals the show in a hilariously demented cameo that is — I’m sorry, but there’s no other way to say it — the comic orgasm of the movie.

But first, the foreplay.

Jason Segel and Cameron Diaz play Jay and Annie, whose boudoir routine has become, well, routine, after having two kids. To spice up their love life, they decide to make a video of themselves having sex, which Jay inadvertently shares when he syncs his iPad with an unknown number of iPads that he has given away as presents. From that point on, the movie is about Jay and Annie’s attempts to track down and delete the copies of the offending video.

But getting to that part takes a while longer than is, strictly speaking, necessary. A substantial chunk of “Sex Tape” is devoted to flashbacks of Jay and Annie in college, when they were, the movie assures us, randy little sex weasels. The movie contains a fair amount of naked derrieres, both his and hers, and contortions you should probably not try at home.

Other scenes introduce some of Annie and Jay’s married friends, including a couple played by Rob Corddry and Ellie Kemper. Although necessary for exposition, these scenes are hardly hilarious.

But there also needs to be a reason for the couple to be so upset about the release of their tape. After all, these days, sex videos are rarely career-enders.

That’s where Lowe enters the picture, in the role of Hank Rosenbaum, the chief executive of a family-oriented toy company that’s considering hiring Annie as a blogger and social-
media marketer. Because the job depends on a wholesome image, Annie is worried that Hank — who was given one of those tainted iPads containing her job proposal — will be too shocked to hire her if he sees the video.

This is when the movie gets good.

Annie and Jay visit Hank’s home, where the plan is for Jay to look for the iPad while Annie distracts Hank. Hank, however, turns out to be a tattooed, cocaine-using freak whose mansion, which is decorated with portraits of Hank in the guise of various animated Disney characters and stocked with sex paraphernalia, is guarded by an attack dog.

Too over the top? Not hardly. This extended sequence, which defies all logic, restraint and good taste, is the film’s centerpiece, and Lowe’s performance is great, good-natured fun.

It’s all downhill from there. Despite another late cameo — by an uncredited actor, the mention of whose name would only ruin the delight of seeing him play a porn entrepreneur — the movie’s last 30 minutes feel anticlimactic, after Lowe’s tour de force.

Segel and Diaz are gifted and game comedians, with a lot of audience appeal. But Lowe clearly upstages them, consummating their “Sex Tape” — and making you want to roll over and have a cigarette — while there’s still one reel to go.

Sex Tape

★ ★ ½

(90 minutes, at area theaters) is rated R for sex, nudity, vulgarity and drug use.