The chemistry of CH3OH and H2CO in thirteen regions of massive star formation is studied through single-dish and interferometer line observations at submillimeter wavelengths. Single-dish spectra at 241 and 338 GHz indicate that T-rot = 30-200 K for CH3OH, but only 60-90 K for H2CO. The tight correlation between T-rot(CH3OH) and T-ex(C2H2) from infrared absorption suggests a common origin of these species, presumably outgassing of icy grain mantles. The CH3OH line widths are 3-5 km s(-1), consistent with those found earlier for (CO)-O-17 and (CS)-S-34, except in GL 7009S and IRAS 20126, whose line shapes reveal CH3OH in the outflows. This difference suggests that for low-luminosity objects, desorption of CH3 OH-rich ice mantles is dominated by shocks, while radiation is more important around massive stars. The wealth of CH3OH and H2CO lines covering a large range of excitation conditions allows us to calculate radial abundance profiles, using the physical structures of the sources derived earlier from submillimeter continuum and CS line data. The data indicate three types of abundance profiles: flat profiles at CH3OH/H(2)similar to 10(-9) for the coldest sources, profiles with a jump in its abundance from similar to 10(-9) to similar to 10(-7) far the warmer sources, and flat profiles at CH3OH/H-2 similar to few 10(-8) for the hot cores. The models are consistent with the approximate to 3 " size of the CH3OH 107 GHz emission measured interferometrically. The location of the jump at T approximate to 100 K suggests that it is due to evaporation of grain mantles, followed by destruction in gas-phase reactions in the hot core stage. In contrast, the H2CO data can be well fit with a constant abundance of a few x 10-9 throughout the envelope, providing limits on its grain surface formation. These results indicate that T-rot (CH3OH) can be used as evolutionary indicator during the embedded phase of massive star formation, independent of source optical depth or orientation. Model calculations of gas-grain chemistry show that CO is primarily reduced (into CH3OH) at densities n(H) less than or similar to 10(4) cm(-3) and primarily oxidized (into CO2) at higher densities. A temperature of approximate to 15 K is required to keep sufficient CO and H on the grain surface, but reactions may continue at higher temperatures if H and O atoms can be trapped inside the ice layer. Assuming grain surface chemistry running at the accretion rate of CO, the observed abundances of solid CO, CO2 and CH3OH constrain the density in the pre-protostellar phase to be n(H) greater than or similar to a few 10(4) cm(-3), and the time spent in this phase to be less than or similar to 10(5) yr. Ultraviolet photolysis and radiolysis by cosmic rays appear less efficient ice processing mechanisms in embedded regions; radiolysis also overproduces HCOOH and CH4.