Imagine writing yourself the role of a lifetime, only to be replaced by a photogenic, puppy-eyed celebrity version of yourself. Such was the fate of Dave Malloy, the writer, composer, and original star of “Natasha, Pierre & the Great Comet of 1812,” an “electro-pop opera” based on a seventy-page sliver of “War and Peace,” which, on Tuesday, was nominated for twelve Tony Awards, the most of any Broadway show this season.

The musical originated in 2012, at the tiny Hell’s Kitchen theatre Ars Nova, with Malloy playing Count Pyotr Kirillovich Bezukhov, otherwise known as Pierre. The next year, the show moved to a pop-up Russian supper club in the meatpacking district, becoming a downtown sensation, followed by a run at American Repertory Theatre, in Cambridge. But, last fall, when the show transferred to Broadway’s Imperial Theatre, the producers needed a big name. Malloy, who is now forty-one, handed over his signature part to everyone’s mother’s favorite tenor, Josh Groban. Now that the show is a hit—with help from the Grobanites, as his fans call themselves—Malloy is stepping back into the role for select performances, starting this weekend. “If you go to buy a ticket on one of my nights,” Malloy said recently, “Ticketmaster will be like, ‘Josh Groban is not performing this night. It’s just this other schlub!’ ”

Malloy doesn’t appear to mind—perhaps because the whole turn of events seems a comic misfortune worthy of Tolstoy’s bumbling hero. Much as Lin-Manuel Miranda felt an unlikely kinship with Alexander Hamilton, Malloy saw himself in Pierre, whom Tolstoy describes as “fat, unusually tall, broad, with enormous red hands.” Downtown, Malloy, who has a gentle demeanor and a bushy blond beard, played Pierre as a kind of oafish hipster m.c.: nineteenth-century Russian aristocrat meets twenty-first-century Greenpoint barista. Groban, meanwhile, has done best to make himself unpresentable for the role, with matted-down hair and a padded belly.

Read more stories from New Yorker writers on the 2017 Tony Awards.

“He’s profoundly socially awkward, which I really, really relate to,” Malloy told me of Pierre, sitting in the back row of the Imperial; moments later, as if to prove his point, he accidentally elbowed his script off a ledge. “But he’s socially awkward in an extroverted way. He goes to these big aristocratic parties and he just doesn’t know how to handle it. He can’t make small talk with people. I could literally be describing myself. I’m terrible at remembering faces, too, which Pierre is as well.” He went on, “But the flip side of all of that is he’s so aware of that about himself that it sends him on this crazy spiritual journey. He’s on this constant spiritual quest to just understand the meaning of life, basically. And he goes through so much—so many versions of what that could look like. He’s looking at religion. He’s looking at drinking. He’s looking at women. He becomes a Freemason for a while, and then he gets into politics and goes to assassinate Napoleon. He’s just going on these crazy journeys. That was very much Tolstoy as well, and certainly that was me in my twenties and thirties.”

Malloy grew up in a half-Latvian family outside of Cleveland, singing in the choir, playing timpani in the wind ensemble and xylophone in the marching band. After studying English and music at Ohio University, he enrolled in grad school for composition, but only made it one semester. Then he moved to San Francisco and took a variety of odd jobs—working at Amoeba Music, the independent record store; teaching preschool; accompanying vocal auditions. Occasionally, he took work playing piano on cruise ships, circling Alaska or the Mediterranean for months on end. Life on the cruises was solitary—he didn’t jibe with the “international frat house” atmosphere—so, while the passengers played slot machines, he read in his cabin or at the Martini bar. In 2007, he bought “War and Peace” at a port. Malloy saw his own artsy social circle in the novel’s princes and countesses. In an e-mail to his girlfriend, back in California—they would read novels together to stay in touch—he wrote, “natasha and andrey man, you have no idea. (ps oh but Pierre! . . . hes all of us, every seeker!)”

The relationship didn’t last (Malloy is now married to the writer and performer Eliza Bent), but his interest in Pierre lingered. He had started dabbling in musical-theatre projects in San Francisco, including a show called “Clown Bible.” Through a guy at the record store, he discovered the experimental troupe Banana Bag & Bodice, with which he created “Beowulf: A Thousand Years of Baggage,” a rollicking klezmer adaptation in which Malloy doubled as Hrothgar and accordionist. The company relocated to New York, and Malloy followed a few years later, landing in an apartment in Crown Heights. In 2009, they staged “Beowulf” at Abrons Arts Center. The Times review said that Malloy’s score “demands to be described as demented.”

He soon found a peculiar niche: dirtying up the Western canon with the louche energy of an East Village dive bar. Unlike, say, “Les Misérables,” “Natasha, Pierre, and the Great Comet of 1812” doesn’t ask us to forget about the present: the performers are simultaneously Tolstoy’s characters and the spunky New Yorkers portraying them. (“You don’t want to lie to the audience,” Malloy explained.) His other musical creations include “Three Pianos,” a wine-soaked love letter to Schubert’s “Winterreise”; “Beardo,” a riff on the legend of Rasputin; and “Preludes,” which was staged at Lincoln Center, set inside the fevered mind of Rachmaninoff. He has toured with his haunted chamber piece “Ghost Quartet” from Bushwick to Edinburgh. And he is close to finishing a “Moby-Dick” adaptation that will mash up a variety of theatrical styles, from a vaudeville highlighting the novel’s whaling arcana to a jazz song cycle about the black cabin boy, Pip. Malloy, naturally, will play Melville.

Of all his projects, “The Great Comet” picked up steam in large part because of its director, Rachel Chavkin, Malloy’s frequent collaborator. They met when Chavkin came to see “Beowulf” and told Malloy she liked his music. Soon after, Malloy asked her to direct “Three Pianos.” Chavkin was, and remains, a whiz at turning theatre spaces into all-consuming environments, as she has done with Off Broadway plays like “Hadestown” (a retelling of the Orpheus myth, staged in the round) and “Small Mouth Sounds” (set at an upstate silent retreat, with the audience on either side of the meditation room).

Malloy had got the idea of setting “The Great Comet” in a night-club-like environment when he went to Moscow to do research for “Beardo.” One night, he went to Café Margarita, a rowdy bar at which a live music trio was playing “Flight of the Bumblebee.” “There were shakers on every table, and people were shaking along to the music and eating dumplings and drinking vodka,” he recalled. “And that night, at that place, I was, like, Oh, this is how ‘Comet’ should work.” At Ars Nova, Chavkin expanded on the idea, transforming the eighty-seven-seat space into a raucous Russophilic soirée.

Eager to pick up on the immersive-theatre craze, which was jump-started by “Sleep No More,” producers moved “The Great Comet” to Kazino, a tent occupying an empty lot across from the Standard Hotel, tricked out with oil paintings, crimson curtains, and twinkling chandeliers. Audience members gorged themselves on caviar and vodka. “We had people getting quite shitfaced,” Malloy recalled. “A woman threw up in her purse during Act II and was carried out, fireman-style, by a security guard.” During one show, a National Review writer became so furious at a distracted audience member that he hurled her cell phone across the room, briefly becoming a self-styled folk hero. The night I saw the show, my friend and I were stopped by cops on our way out—they suspected we’d been up to no good in the lot, until they went in and discovered that our only crime had been attending a Tolstoy musical.