The magical teddy bear with the heart of gold and the mouth of potty is back in “Ted 2,” and so is writer-director-star Seth MacFarlane’s mischievous mojo, which went missing somewhere in last year’s mirthless comic Western “A Million Ways to Die in the West.” A sequel to MacFarlane’s surprise 2012 smash ($549 million worldwide), “Ted 2” is surely the last movie one would expect to find quoting from the anthropologist Dawn Prince-Hughes’ writings on the essence of human consciousness. But in its own, sweetly subversive way, this might be just the tolerance plea America needs right now — a movie that says, in effect, “Love thy plushie as thyself.” Fret not: Such high-mindedness has little diminished MacFarlane’s appetite for locker-room humor, gross-out sight gags and bounteous pop-culture in-jokes, which should make “Ted 2” the season’s go-to attraction for arrested-adolescent males of all ages, and continue Universal’s beary good summer box office.

The last time we saw Ted (voiced by MacFarlane), he’d been brought back from the brink of death and was serving as best man at the wedding of his lifelong “thunder buddy,” John (Mark Wahlberg), and bride Lori (Mila Kunis). When the sequel picks up, it’s Ted’s turn at the altar, where he trades vows with brassy checkout girl Tami-Lynn (the terrific Jessica Barth, who’s like a trailer-park Judy Holliday) while returning guest star (and erstwhile Flash Gordon) Sam J. Jones once again serves as officiant. It’s a fitting start for a movie that acknowledges its own inescapable sequel-ness at least as often as “Jurassic World,” repeating some gags, inverting others, and lining up a new raft of “I can’t believe that’s really … ” celebrity cameos.

Alas, romantic bliss is not long for either of these Beantown couples. By the time Ted says “I do,” John is already six months divorced from Lori (a convenient way of explaining the absent Kunis, whose real-life pregnancy coincided with the shoot). One year after that, Ted’s own once-happy home has become a kitchen-sink melodrama of tears and recriminations (including the amusing notion that Tami-Lynn has been blowing too much of the family budget on shopping sprees at Boston discount retailer Filene’s Basement). The only way to save their relationship, Ted reasons, is for him and Tami-Lynn to have a child — a plan that faces only one small hitch: the father’s lack of anatomical correctness.

That sets the stage for a delightfully crackpot artificial insemination scheme that sees Ted and John plotting to make off with the sperm of New England Patriots QB Tom Brady (gamely playing himself). But it’s only during a later meeting with an adoption counselor (slyly modeled on a similar scene from Michael Mann’s “Thief”) that Ted discovers erectile dysfunction to be the least of his problems. The state of Massachusetts has determined that he is a piece of property, not a person — and, before you can say Dred Scott, Ted stands stripped of his identity, his job and his marriage certificate.

What remains intact is Ted’s sense of self, and so “Ted 2” evolves (to use the term loosely) into a MacFarlanized courtroom drama in which man and anthropomorphic stuffed bear struggle to find common ground. Helping in that battle is eager young civil rights attorney Sam(antha) L. Jackson (Amanda Seyfried), a recent law-school grad who wins over her first clients by demonstrating that she knows her way around a bong. (Well, that and the fact that her name is Sam L. Jackson.) As it turns out, Jackson knows her way around a courtroom, too, just as Seyfried (who was one of the few bright spots in “A Million Ways to Die in the West”) cheerfully fields whatever new humiliation (including a running bit about her resemblance to Gollum) MacFarlane lobs her way. She brings a warmth and sweetness to the film that balances nicely against the bad-boy crudity, and even croons a lovely original ballad, “Mean Ol’ Moon,” during a campfire scene set on (what else?) a marijuana farm.

The trial scenes (featuring “Mad Men’s” John Slattery for the prosecution) drag on a bit, as does pretty much everything else in this 115-minute extravaganza, suggesting that MacFarlane and co-writers Alec Sulkin and Wellesley Wild don’t always know their A material from their B material. That’s especially true of an expendable subplot that sees erstwhile stalker Donny (Giovanni Ribisi), now working as a janitor for Hasbro, once again plotting to kidnap Ted and cut him open to see what makes him tick. In fact, outside the two leads, most of the returning cast are given precious little to do this time around, including Jones and Patrick Warburton (as Wahlberg’s gay bully co-worker). But the movie makes better use of new additions Morgan Freeman (parodying his saintly airs as a storied civil rights lawyer) and Liam Neeson (as a grocery shopper with some most unusual concerns about Trix cereal).

Both “Ted” movies are, ultimately, one-joke affairs rooted in the idea of taking some emblem of childhood innocence and vulgarizing it (like Stewie, the nefarious infant from MacFarlane’s “Family Guy” series). That joke, though, turns out to be a resilient one, and the chemistry between Wahlberg and MacFarlane is infectiously puerile, whether they’re playing an ill-conceived game of catch in a sperm bank’s storage room or shouting out “sad suggestions” during a night of improv comedy. The visual effects responsible for transforming MacFarlane’s on-set motion-capture performance into his 2-foot-tall alter ego have once again been seamlessly executed. Special mention is also due Tony-winning director-choreographer Rob Ashford for staging the film’s opening title sequence — an elaborate, Busby Berkeley-style dance number featuring a tuxedoed Ted and a chorus line of dancers in geometric configurations atop a giant wedding cake. Can a full-blown Seth MacFarlane musical be long in the offing?