We theoretically investigate the structural and thermoelectric properties of magnesium silicide (Mg2Si) incorporating Al or Sb atoms as impurities using first-principles calculations. We optimized the structural properties through variable-cell relaxation using a pseudopotential method based on density functional theory. The result indicates that the lattice constant can be affected by the insertion of impurity atoms into the system, mainly because the ionic radii of these impurities differ from those of the matrix constituents Mg and Si. We then estimate, on the basis of the optimized structures, the site preferences of the impurity atoms using a formation energy calculation. The result shows a nontrivial concentration-dependence of the site occupation, such that Al tends to go into the Si, Mg, and interstitial sites with comparable formation energies at low doping levels (<2 at.%); it can start to substitute for the Mg sites preferentially at higher doping levels (<4 at.%). Sb, on the other hand, shows a strong preference for the Si sites at all impurity concentrations. Furthermore, we obtain the temperature-dependence of the thermoelectromotive force (Seebeck coefficient) of the Al- and Sb-doped Mg2Si using the full-potential linearized augmented-plane-wave method and the Boltzmann transport equation.