Patrick Ryan

USA TODAY

In A Ghost Story, the most unexpected guest at a suburban house party isn't a bedsheet-clad phantom.

Instead, it's Kesha, the glittery pop rebel of Tik Tok fame who has been caught up in a legal battle with mega-producer Dr. Luke, whom she has accused of sexual assault. The singer/songwriter makes a blink-and-you'll-miss-it appearance midway through Ghost Story (in theaters Friday in New York and Los Angeles, expands to additional cities through July) at a get-together in the former Dallas home of the late C (Casey Affleck) and his partner, M (Rooney Mara).

You'll never look at pie the same after Rooney Mara's 'Ghost Story' scene

The heady drama's director, David Lowery, considers himself a Kesha superfan, citing Die Young, Warrior and C'mon as some of the "most anthemic pieces of pop in the last 20 years." He hoped to use a Kesha song in a party sequence, but wound up getting her to not only appear in the movie but also co-write a tune that plays in the background.

The cameo was a "crazy coincidence," Lowery says. "(A) friend was at a party that Kesha just happened to be at and was like, 'Hey, I've got some friends in Texas and they want you to be in their movie.' Two days later, she flew to Texas and did it. It was so last-minute and spontaneous that I was really worried that she'd get there and be disappointed and realize that she had very little to actually do in that scene. But we wanted her to be there. Thankfully, she did it and had a blast."

In the scene, professional magician Jared Kopf does magic tricks for Kesha, who is credited as "Spirit Girl" in the movie.

"All of the dialogue is just him performing and her reacting naturally," Lowery says. "We set up a camera and shot it live, so it's 100% just her. But in part of his routine, he always invokes a spirit. He knew what the movie was about, so he was playing it up and she was responding. It's very on the nose on one level, but also very appropriate on another."

Kesha's song, titled Last One and performed by Detroit twin-sister duo Stereo Jane, was co-written with Toby Halbrooks and Andrew Tinker, who also wrote music for Lowery's Pete's Dragon remake last year. "They had written a beat with a hook and asked her if she would finish it," Lowery says. "She went and wrote the rest of it, and it was just kind of a dream come true."

Review: Elegiac and moving, 'A Ghost Story' will haunt you

Sundance: 5 secrets behind Casey Affleck's bedsheet-clad 'Ghost Story'

The filmmaker hopes it's not the last time he collaborates with the singer and occasional actress, who has also guest-starred on TV shows including Nashville, Jane the Virgin and Animals.

"I'd love to make a movie just starring her. She's a wonderful presence and a wonderful human being," Lowery says. While he hasn't nailed down an idea for what that film would be, "I know she loves whales and has a giant whale tattoo, and whales are my favorite animal. So maybe something involving seagoing creatures. I haven't thought that far ahead, but I could spend 20 minutes just thinking about her personality and her spirit, and a story would emerge."