There are so many ways this movie could have gone wrong that it’s a minor miracle it never flies off the rails. A story about an emotionally stunted man whose obsession with a life-sized doll he purchases over the Internet comes to the attention of the small town where he lives? The possibilities for shameless crudity and the comedy of embarrassment seem endless. And directed by Craig Gillespie, who made his debut with the decidedly unsubtle “Mr. Woodcock”? The possibilities for catastrophe suddenly multiply.

But the surprise is that “Lars and the Real Girl” turns out to be a sweetly quirky, warmly funny and even touching tale of how a community supports one of its more fragile members. It’s reminiscent of a television program like “Northern Exposure,” or to go even further back, the old “Andy Griffith Show,” in which tiny burgs not only tolerated but even cherished their eccentrics. Lars Lindstrom goes further than most of the oddballs in those locales did, of course, but like them the place where his story is set—its precise location is never stipulated, but it sure looks a lot like Minnesota—is less real than fairytale.

Even on those terms, of course, the movie could have stumbled badly, turning mawkish and sappy. But it avoids that very different pitfall, too, managing to maintain a tone that’s humorous without becoming merely jokey, poignant without being maudlin, and offbeat without becoming merely annoying. It does a balancing act that’s really quite remarkable, and manages an ending that’s satisfying without becoming saccharine.

There are several reasons for this. A major one is certainly the agility of Gillespie and scripter Nancy Oliver in balancing the apparently ordinary, matter-of-fact setting with the peculiarities of the people and the plot, and their deft juxtaposition of laid-back sight and verbal gags (some quite hilarious) with moments of intense emotional honesty. Another is the skill of the supporting cast, from Paul Schneider and Emily Mortimer as Lars’s concerned brother and sister-in-law and Patricia Clarkson as the town doctor who suggests a radical treatment of his psychological problem to Kelli Garner as the new worker in Lars’s office who tries to romance the preternaturally shy guy and Nancy Beatty as the gruffly supportive neighbor who encourages everyone else to help him.

But certainly the greatest single element in the picture’s success is Ryan Gosling, who makes Lars sympathetic rather than simply pathetic and captures the character’s mingled pain and neediness with consummate skill. It’s a rich and endearing performance, especially in his scenes with his girlfriend Bianca, which could have turned into something grotesque rather than affecting. But he plays off well against all the cast members.

This is a modest, almost self-effacing, film from a technical perspective, but Adam Kimmel’s cinematography catches the wintry feel of the locations nicely, and the sense of place is palpable in Arv Grewal’s production design, Joshu de Cartier’s art direction and the costumes—plain but just right—by Kriston Mann and Gerri Gillan.

“Lars and the Real Girl” is a small film with a story that may initially put off some viewers but will eventually win most of them over with its quiet but big-hearted charm. It may be about a fellow who constructs his own reality, but its winning character is no delusion.