These kids today, with their flashlights.

A theme running through most of Todd Haynes’ films (“Velvet Goldmine,” “Far from Heaven,” “Carol”) is one of breaking loose from convention to be true to oneself. Often this breaking-loose has to do with sexuality, but the wonderful “Wonderstruck,” based on the illustrated novel by Brian Selznick (“Hugo”), applies it to marginalized children trying to find their place in the world. It’s a charming, magical story, full of simple surprises and innocent beauty.

It starts out as two stories, actually. In Minnesota in 1977, 12-year-old Ben (Oakes Fegley, from last year’s “Pete’s Dragon”) never knew his father and recently lost his mother (Michelle Williams) in a car accident. He finds an old book among her belongings — “Wonderstruck,” about New York’s American Museum of Natural History — and a bookmark with an inscription to Ben’s mother from someone named “Danny.” Could that be Ben’s father? While dialing the telephone number of the bookshop indicated on the bookmark, Ben is struck by lightning and loses his hearing, as if the universe didn’t want him to make the connection. Doesn’t matter. He sets out for New York to find the bookstore and, he hopes, his father.

Meanwhile, in Hoboken, N.J., in 1927, a deaf, lonely rich girl named Rose (Millicent Simmonds) idolizes movie star Lillian Mayhew (Julianne Moore) and dreads the coming day when talkies will take over, spoiling her refuge. Fed up with her disinterested father (James Urbaniak), who seems annoyed with her for being deaf, Rose heads into Manhattan in search of Ms. Mayhew, who’s rehearsing for her Broadway debut. It isn’t as long a journey for Rose as it is for Ben — New York is just a ferry ride away — but it’s a grand adventure for a 12-year-old deaf girl in 1927.

Haynes cuts back and forth between time periods regularly, underlining the parallels in the stories — both kids trek to New York and face dangers in the big city; both end up at the American Museum of Natural History — without indicating how they are related. The 1977 scenes have the rich, saturated colors and funky soundtrack of movies from that time, and while Ben can’t hear the hustle and bustle around him, we can. We can practically smell it. To remind us that Ben is deaf, Haynes occasionally lets the dialogue drop from the sound mix.

The 1927 scenes, on the other hand, evoke the silent film era without duplicating it. The widescreen images are sepia-toned. We don’t hear any dialogue (you may be surprised at how good you are at lip-reading), and there’s a near-constant musical score — but the music, by the great Carter Burwell, is modern, not era-appropriate. (I like to imagine that it’s the music Rose hears in her head, accompanying her own life.) Burwell’s score is gorgeous and yearning, and Haynes uses it across both time periods, tying them together even while we’re still in the dark about their connection.

The parallel tracks do eventually merge (this is not a puzzle movie), focusing on Ben’s search for his father. He’s helped by a new friend, Jamie (Jaden Michael), an eager lad without many friends whose own father works at the museum. Jamie is an endearing type, the good-hearted character whose only function in the story to help the protagonist and be his pal. His and Ben’s sweet friendship anchors the movie.

Selznick, adapting his own book, has a storyteller’s gift for parceling out information. He reveals biographical details one piece at a time, letting us enjoy the process of discovery in a manner similar to how Ben and Rose are learning more about their world. They’re not after the same facts we are, but the path is the same: curiosity, wonderment, the joy of finding out something new. The movie is a celebration of all that, framed within a story about kids doing neat things like hide out in a museum after hours. This is a lovely, euphoric movie, made with skill and compassion.

A- (1 hr., 57 min.; PG, mild thematic elements.)