H2S can come in from the well. It can also be produced in the water heater.
As I understand it, SRB (sulfate reducing bacteria) can break down sulfate, but need metal ions to help with their operation. Magnesium works best of metals in WHs. Some other metals can work too, but not as well.
To stop this from occurring, you can use a powered anode, you can use a WH that does not need a sacrificial anode (plastic or stainless), you can remove the anode and insert a plug instead (accepting reduced WH life). You can make the water hotter, you can kill off the SRB. Sanitizing well and plumbing can help for a while. How long? Month, year, years? I expect that varies, but the better the sanitizing, the longer the effect I think.
Lot of stuff I don't know on this. Lots of stuff I have read. I would like to read more on it.
I have an H2S+iron backwashing filter, and I have a Ceranode powered anode. The H2S was my motivation more than the iron, but glad to be rid of the iron too. My iron was not too bad. My H2S was not too bad compared to some, but I did not want it. My media is about 7 years old, and I expect to need to change it some day.