>>38115621
First and foremost, doujin culture is people sharing common interests getting together to produce what they want. It can be any kind of media, music, books, animation, 3D modeling, cosplay, jewelry... It doesn't have to be based of an existing IP and there's a lot of original work, but since enjoying the same series is a big factor for people to group together it's not suprising there's a huge amount of that too. Then those people sell their work at various events through the year, the biggest one being Comiket. And for work based on an IP, that is illegal.
>Or the IP's owner has all the rights to decide what happens to that music album?
They have all the rights to do that, just as in the west. However most of the time the law isn't enforced because everyone has something to gain here. If you find a music CD or doujinshi and you like it, chances are you'll want to check out the source material at some point so it's basically a huge free publicity for the companies being it. Plus a lot of artists first begin by producing and selling doujin work before working in the industry, so if lets say a manga publisher were to take down doujinshi based of their IPs it would send a big middle fingers to both the customers and the artists, and surely if one of them end up being the author of the next huge hit and they're looking for a publisher, they won't choose that one.
There's exceptions though. I remember not so long ago the production company behind Girls und Panzer banning any derivative work of the IP. It wasn't well received. And as you said, Nintendo is also especially infamous for taking down everything, you won't find Nintendo-based doujin work at Comiket but they definitely don't need any kind of free publicity so I guess they just don't care. As for Touhou, as long as you follow the very permissive guidelines you're free to do pretty much whatever you want. ZUN still have all the rights on his IP and can take down projects which doesn't follow the rules, it happened a couple of times like with that ufotable anime.
tl;dr Selling doujin work based of an IP is illegal but for the law to be enforced you first need right owners to complain, and most don't as they actually benefit from the situation.