An expression that describes the upper critical magnetic field H (c2) and generalizes the Gor'kov relation has been derived for the two-band two-gap superconductor MgB2. The expression relates the upper critical magnetic field H (c2) to the residual resistivity and the parameters of the band structure and holds in the range from the clean limit to the dirty limit. The ratios of the relaxation times tau(pi)/tau(sigma) and the mean free paths of pi- and sigma-band electrons for MgB2 samples with a low defect level and Mg(B1 - x C (x) )(2) samples with a partial substitution of carbon for boron are determined from experimental data.