I don't necessarily think that it has to be a castle level, but I know that you CAN'T end the level with the goal tape or goal sphere. If you count the Big Boo boss and Reznor as bosses, then yes, you can only activate the level end with a boss. Iggy, Morton, Lemmy, Ludwig von Koopa, Roy, Wendy, Larry, Bowser, Reznors, and the Big Boo are all acceptable. Now using custom bosses is a whole different story. I have no idea there.
Now for some of the expert hackers who use hex editors, ASM hacking, or ripping and designing anything else, they probably can make it that you don't have to end with the boss battle of your choice. Maybe triggering or changing the level address, level mode, or something, I have ABSOLUTELY NO IDEA!!!
Well, I hoped this helped.
Look at this 8 year-old layout that I should probably update.