Context. OCS and SO2 are both major carriers of gaseous sulfur and are the only sulfurated molecules detected in interstellar ices to date. They are thus the ideal candidates for exploring the evolution of the volatile sulfur content throughout the different stages of star formation. Aims. We aim to investigate the chemical history of interstellar OCS and SO2 by deriving a statistically significant sample of gas-phase column densities toward massive protostars and comparing them to observations of gas and ices toward other sources, from dark clouds to comets. Methods. We analyzed a subset of 26 line-rich massive protostars observed by ALMA in Band 6 as part of the High Mass Protocluster Formation in the Galaxy (ALMAGAL) survey. Column densities were derived for OCS and SO2 from their rare isotopologs O13CS and 34SO2 toward the compact gas around the hot cores. We compared the abundance ratios of gaseous OCS, SO2, and CH3OH with ice detections toward both high- and low-mass sources as well as dark clouds and comets. Results. We find that gas-phase column density ratios of OCS and SO2 with respect to methanol remain fairly constant as a function of luminosity between low- and high-mass sources, despite their very different physical conditions. In our dataset, OCS and SO2 are weakly correlated. The derived gaseous OCS and SO2 abundances relative to CH3OH are overall similar to protostellar ice values, with a significantly larger scatter for SO2 than for OCS. Cometary and dark-cloud ice values agree well with protostellar gas-phase ratios for OCS, whereas higher abundances of SO2 are generally seen in comets compared to the other sources. Gaseous SO2/OCS ratios are consistent with ices toward dark clouds, protostars, and comets, albeit with some scatter. Conclusions. The constant gas-phase column density ratios throughout low- and high-mass sources indicate an early-stage formation before intense environmental differentiation begins. Icy protostellar values are similar to the gas-phase medians and are compatible with an icy origin for these species followed by thermal sublimation. The larger spread in SO2 compared to OCS ratios with respect to CH3OH is likely due to a more water-rich chemical environment associated with the former, as opposed to a CO-rich origin for the latter. Post-sublimation gas-phase processing of SO2 can also contribute to the large spread. Comparisons to ices in dark clouds and comets point to a significant inheritance of OCS from earlier to later evolutionary stages.