In the beginning, theater stages did not have curtains, so actors had to be off the stage somehow at the end of each act. If a character died, they certainly didn't want the corpse getting up and walking off, so the playrights invented the funeral procession to carry the body away.
"The Hearse Song" as it is called by historians was first documented in the 19th century, but many people think it dates from WW1. And that's as good as the story gets.
BTW Alfred Hitchcock's theme is a funeral march from opera.