Firefighter William Tolley, who fell five stories to his death Thursday, was trying to get his ladder bucket unstuck from a rooftop ledge after putting out a routine fire caused by unattended incense, FDNY sources told The Post.

Tolley, 42, was moving from a ladder bucket onto the roof of the building at 1615 Putnam Ave. around 3 p.m. — as fellow Bravest battled a 2-alarm fire on the building’s second floor.

But the bucket got caught on the edge of the roof, and firefighters were trying to reposition it, sources said. Then, the ladder jerked and Tolley plummeted five stories to the concrete below, according to sources and video.

Tolley died at Wyckoff Heights Medical Center of multiple blunt-force injuries, according to a medical examiner’s report.

Tolley was going to inspect the roof in what Fire Commissioner Daniel Nigro said Thursday was a “routine operation.”

The 14-year FDNY veteran left behind wife Marie and 8-year-old daughter Bella, officials said.

Incense left burning unattended caused the fire, Nigro said on Friday.

“Compounding the tragic loss of Firefighter Tolley’s life is that the fire he responded to and fought bravely could have been prevented,” said Nigro. “You should not leave objects such as incense or candles burning while unattended, and I urge all New Yorkers to follow these safety rules to prevent a tragedy in the home.”

Neighbors gathered outside Tolley’s Bethpage, Long Island, home in somber remembrance on Friday morning.

“The guy was just tremendous,” said Mike Gleason, 69. “He was a great neighbor an outstanding firefighter and I feel terrible for his wife and daughter.”

And more than 50 fellow smoke eaters lined up outside Bellevue Hospital on Friday to see Tolley’s casket — draped in an American flag — loaded into an ambulance before it was transported to a Long Island funeral home.

Tolley visited a favorite bakery of his in search of a symbolic dessert for his daughter’s First Holy Communion just hours before his death.

The family man stopped at Glendale’s Mahalo New York Bakery — which is next door to his firehouse, Ladder 135/Engine 286 — before he was called to the minor fire, the owner said Friday.

“He was on shift…We knew him as Billy, and he was in here looking for something for a communion, specifically a cross made out of cupcakes,” owner Sunita Shudin said.

The cupcakes were for daughter Bella, according to Chris Pervelis, who played guitar in a band with Tolley.

“I know he went to church every Sunday with Bella,” Pervelis told The Post. “He was not incredibly religious, but he wanted his daughter to experience religion, and he always said to me ‘It’s her choice in the end, but I want her to be exposed to it.’”

On the day of his death, Tolley and Pervalis’ wife were also planning a surprise birthday for the guitarist. The wife sent Tolley a picture of a cake that she wanted, and he said he could get it from a bakery near his firehouse, texts shared with The Post show.

Tolley sent Pervalis’s wife a photo of a cake decorated to look like Tequila bottle and cigars at 2:38 p.m. on Thursday.

“Make Chris a cake like this except using a Pappy [Van Winkle] Bottle cake,” Tolley wrote just minutes before his death. “This bakery is right next to my fire station.”

Additional reporting by Abigail Gepner and Leonica Valentine