Disney XD is saying so long to its highest-rated series tonight with the hourlong finale of “Gravity Falls.”

In wrapping the animated series with its 40th episode, series creator/exec producer Alex Hirsch joins the growing list of showrunners afforded the luxury of setting their own end date.

The fact that Hirsch pulled it off with the first series he ever created speaks volumes about Disney’s faith in the animation wunderkind, now 30. “Gravity Falls” pushed boundaries big-time for Disney Channel and Disney XD as a highly serialized mystery-thriller that weaves an apocalyptic conspiracy story around the coming-of-age adventures of 12-year-old twins Mabel and Dipper Pines.

The brother and sister are sent to spend the summer with their great-uncle Stan, who runs a roadside attraction in a small town in Oregon where strange things happen. The show made the most of its animated format by peppering secret clues, codes and other Easter eggs into episodes to keep fans hunting for hints into the larger mythology.

The characters are loosely based on Hirsch and his own twin sister. He developed “Gravity Falls” with a specific story arc in mind, one that concludes tonight at 7 p.m. with “Weirdmageddon Part 3: Take Back the Falls.”

“From the very first I pitched the show as this thing that has only a few seasons — it has a beginning, middle and end. It’s one summer. There are just a few big questions that have answers, and once they’re answered, that’s it,” Hirsch said on Feb. 10 during a Q&A with voice cast members Jason Ritter (Dipper), Kristen Schaal (Mabel) and Brad Joseph Breeck, composer of the show’s theme song.

“I don’t think anyone (at Disney) believed me but they said ‘Sure let’s give this a try,‘ ” Hirsch said. “I’m grateful to the channel for letting me tell the story the way I planned to tell it. … I think we’re in a different age of TV right now than that old classic million-season concept. I think audiences are craving different kinds of entertainment.”

At the outset, Hirsch was worried that the show’s conceit would be a hard sell with the Disney Channel/Disney XD kid demo. (“Gravity Falls” premiered in 2012 on Disney Channel and shifted to Disney XD in 2014). But he was proven wrong when the show became a hit thanks in part to its multigenerational appeal.

“Gravity Falls” was Disney XD’s most watched series last year across all target demos with an average of 1.8 million viewers per episode. In kids 6-11, it averaged 654,000 viewers and 790,000 in kids 2-11. Among boys 6-14 it pulled in 680,000. That’s a strong showing in Disney XD’s core demos but it also makes clear that older teens and young adults comprised more than half of the show’s audience.

The “Gravity Falls” hip quotient was reflected in the guest stars who provided voices for the show, a roster that includes Nathan Fillion, Jon Stewart, Louis C.K., Jonathan Banks, John Oliver, Patton Oswalt, Neil deGrasse Tyson, Weird Al Yankovic, Alfred Molina and Nick Offerman.

But being on Disney Channel and Disney XD meant that connecting with the pre-teen audience was of paramount importance. Hirsch has marveled at how passionately kids embraced the show in all its scary weirdness. He credits Disney with letting them push the envelope on the tone and the storylines. The finale finds Dipper and Mabel in a battle with the show’s arch-villain, Bill Cipher (who is shaped like the pyramid found on $1 bills) to save Gravity Falls (and by extension, the rest of the planet) from his evil domination.

During the run of the series, “I would keep daring everyone to tell me I had gone too far with scary stuff,” Hirsch said. But the audience went along for the ride. “Gravity Falls” has generated the kind of fanatical online following of decidedly more grown-up mystery dramas a la “Lost” or “The X-Files.” That kind of engagement also marked new territory for a Disney Channel/Disney XD series.

“You don’t have to sugar coat things for kids. If you make something for them with intelligence they will show that intelligence in ways that will sometimes shock you,” Hirsch said. “They understand we’re not being cruel. We’re creating something with stakes. No matter how scary we made it, they loved it.”

Beyond the thrills and chills, Hirsch and his core writers and producers — Robert Renzetti, Josh Weinstein, Mark Rizzo, Matt Chapman, Jeffrey Rowe, Shion Takeuchi and Michael Rianda — took care to build a deep emotional storyline for the central characters, one that resonated with its target audience. The twins’ supernatural summer adventures are also a vehicle for showing how they navigate the awkward transition into adolescence.

“Mabel is learning that she’s not going to be a kid forever — there’s a piece of her innocence that is lost,” Schaal said. “You’re going to lose a little piece of yourself in every phase of life. That’s a hard lesson to learn. The show eloquently draws that out” of the characters, Schaal said.

Hirsch and the other cast members were shocked at how quickly the show caught on. They were startled to see fans dressed as Mabel and Dipper at the show’s first appearance at Comic-Con in 2012, which came after only four episodes had aired. After a few more episodes aired, Hirsch was sent a fan-generated elaborate rap tribute to the show that chronicled every plot point in great detail.

“When I got that I thought ‘Oh my god, anything can happen’ if someone who is clearly an adult cares enough to make something like this,” Hirsch said.

Seeing the show through to its logical finale — the end of the twins’ wild summer vacation — is satisfying for Hirsch. With “Gravity Falls” wrapped, he’s graduated to a new phase of his career by recently signed a development deal for a new animated project with Fox.

Hirsch knows there’s an element of the fan base that would like to see the show continue. He allows that it’s “not inconceivable” that he would return to the Falls someday, and he hints that there are plot threads in the finale that could set up a future installment. But for now, it was time to take Mabel, Dipper and Grunkle-Stan out on a high note.

“I did this because I love the show. I didn’t want us to ever run out of mysteries. I didn’t want to see Mabel and Dipper have to set there in front of the TV and shrug,” he said. “For me it’s most important to do something that I believe in, something I’m proud of, something I can stand up and say ‘That was my plan’ rather than stretch it out.”

Although success came quickly to Hirsch on the heels of his graduation from CalArts — the art and design school that has long been a farm team for Disney — he recognizes his good fortune in having such a charmed experience with “Gravity Falls.”

“I tried a crazy experiment and the craziest thing is, they let me do it,” he said.

Here’s an exclusive video clip featuring “Gravity Falls” guest stars Nathan Fillion, Weird Al Yankovic, Alfred Molina and Neil deGrasse Tyson discussing their work on the show. Special thanks to the devotees at Disney’s D23 fan club for assistance with this report.