So back to the original question, do they have some sort of a class for that? or do they leave it for experience?
Some schools, including the one I work at, do have formal educational sessions about this. However, there is only so much you can learn from simulations, discussions and lectures, especially about this.
Neonatologists deal with death all of the time. Typically I have at least 1 or 2 deaths every month that I work in the NICU, sometimes more. I've been at this as an attending for over 20 years, so I've done it a lot.
What I've learned is that dealing with the impending death, actual death and the post-death discussions with families, although not enjoyable, is one of the most rewarding aspects of my job. My view is shared by many of my colleagues and some even have focused on things like neonatal hospice care and bereavement.
Why? Well because like everything else we do, and in some ways more than many things we do, the attitudes, caring and skill of the physicians, nurses and others in this situation makes a huge difference. Done properly and compassionately, families have a very different experience than when not done this way. They tell us this over and over again.
A simple example to help understand this. In telling a family that there is no benefit to (more) CPR on a baby who is now certain to die, saying things like "We've done all we can for your baby and more things like chest compressions will not be helpful, now we want you to hold your baby while we give {insert baby's actual name} medicine to make sure {insert correct gender pronoun} is not having any pain." beats the heck out of "Is it okay with you if we stop doing chest compressions on {insert wrong gender pronoun} because they aren't working. Your baby is going to die anyway."
Satisfaction also comes from doing autopsy discussions with families months after a baby's death and answering all of their questions.
In the end, training in these things takes time, experience, comfort with ones one medical practice, and a real understanding of the blessings to a family that compassionate care surrounding the time of a baby's death means to them.
Trainees at every level learn this as they do everything else. See one, do one, teach one. Or more than one.