It was the stuff of a great pop song: On a sunny Saturday afternoon earlier this month, Taylor Swift, one of music’s biggest stars and a notorious romantic, joined an unsuspecting couple’s wedding in Long Beach Island, N.J.

“I apologize for wedding crashing,” Ms. Swift said to the couple, Max Singer and Kenya Smith, and about 100 guests, before sitting down at a piano and performing her hit song “Blank Space.”

Judging by the reaction — shocked gasps, shrieks and one man repeatedly shouting “Oh, my God!” — her unannounced entrance was quickly forgiven.

Interactive Feature | Taylor Swift Surprises a Couple at Their Wedding

Though the surprise appearance made headlines and spurred breathless tweets and Instagram posts, Ms. Swift was not the couple’s first choice for guest of honor. That distinction had been reserved for the groom’s mother, Janey Singer, who died in November at 62, two months after being diagnosed with lung cancer.

The groom’s sister, Ali Singer, said in an interview that the couple decided to marry quietly last year, in a hospice care center with Mr. Singer’s mother. There were no rings exchanged and no walk down the aisle, but there was music. The groom and his mother danced to “Blank Space,” one of his favorite songs. The bride’s father and stepmother watched on FaceTime from home in Iowa City. Mr. Singer’s mother died days later.

“Her selflessness,” Ali Singer said of her new sister-in-law, “to take a day where it’s all about you, and drop it all to get married in a room so my mom could be there, was incredible, and really showed their true partnership together.”

It was not the wedding the couple had envisioned, and they wondered if they should keep their original plans for a June wedding as well. Ms. Singer, who works in advertising at Facebook, helped take over the planning from her mother, and said she was inspired by words of support given to Sheryl Sandberg, Facebook’s chief operating officer, after Ms. Sandberg’s husband died last May.

“Option A is not available,” a friend had said to a grieving Ms. Sandberg, before suggesting she enthusiastically embrace Option B. The couple decided to continue with the June 4 wedding.

The couple’s ceremony was held Saturday at the Brant Beach Yacht Club on Long Beach Island, about two hours south of New York City. The bride, 26, was escorted down the aisle by her father, Brian Smith. The guests dined on a raw bar, ate a surf and turf dinner and enjoyed music from a D.J., who was definitely upstaged.

The plans for the surprise began when a family friend heard that Mr. Singer, 29, had read his graduate school application essay to his mother weeks before she died. An Army captain who had served in Iraq and Afghanistan — and an unapologetic fan of Ms. Swift’s — Mr. Singer decided to break up his essay into themes, each related to song titles from Ms. Swift’s “1989,” an album about the myriad ways a heart can break.

The family friend asked Mr. Singer for a copy and, unbeknown to anyone, sent a batch of letters to people she hoped could help reach Ms. Swift.

(Mr. Singer’s essay choice paid off: He was accepted to Cornell University, and the newlyweds will soon move from Colorado Springs, to Ithaca, N.Y.)

Ms. Swift, whose career suggests she knows a good love story when she sees one, was a last-minute R.S.V.P., approving the request days before the wedding. But her appearance, which included a handmade picture for the couple, “restored everyone’s faith in humanity,” Ms. Singer said.

Interactive Feature | Taylor Swift Surprises a Couple at Their Wedding

In a video of the surprise performance, Ms. Swift, dressed in a strapless dress covered with florals in sunset colors, paused briefly to say that she never thought she’d be singing the song, about many loves gone sour, at a wedding. But, she said, “what Max and Kenya want, they get.”

Despite the attention, the couple have largely shied away from the media attention surrounding Ms. Swift’s visit. In an interview, Mr. Smith, 53, stressed that his daughter, a teacher originally from Sigourney, Iowa, was a private person who “doesn’t want to be bugged” about her wedding, the details of which have been splashed across the internet by relatives, friends and Ms. Swift, who posted pictures of herself with the couple on Instagram.

But Mr. Smith did have this to offer about the love story behind the wedding seen around the world: “The first time she brought him back to Iowa to meet us, we knew right away that this would be the one.”