Boiled eggs turn black inside, specifically around the yolk, due to a chemical reaction between iron in the yolk and hydrogen sulfide in the white, which is accelerated by heat and prolonged storage in the refrigerator. This reaction forms iron sulfide, which is the black substance you see.
When you boil an egg, the heat causes proteins in both the white and the yolk to break down. The egg white contains sulfur-containing amino acids. During cooking, these amino acids release hydrogen sulfide gas. The yolk contains iron. When hydrogen sulfide comes into contact with iron, it reacts to form iron sulfide. This reaction is more likely to occur, and more pronounced, if the eggs are overcooked or left to cool slowly. Refrigeration slows down many chemical reactions, but it doesn't stop this one entirely. Over time, the iron sulfide will continue to form, leading to the black or greenish-black ring around the yolk. The color change is harmless and doesn't affect the egg's safety for consumption. The intensity of the discoloration depends on factors like the cooking time, the cooling method, and the iron content of the egg yolk.
To minimize the formation of the black ring, avoid overcooking the eggs. After boiling, immediately cool them down in ice water. This rapid cooling helps to prevent the hydrogen sulfide from reacting with the iron in the yolk.