We have studied the structural, electronic, and lattice dynamical properties of ${\text{Mg}}_{1\ensuremath{-}x}{\text{Al}}_{x}{\text{B}}_{2}$ and ${\text{MgB}}_{2(1\ensuremath{-}y)}{\text{C}}_{2y}$ alloys within the framework of density-functional theory using the self-consistent virtual-crystal approximation (VCA). The structural properties, electronic band structure, and full phonon dispersion have been analyzed for the $0\ensuremath{\le}x(\text{Al})\ensuremath{\le}1$ and $0\ensuremath{\le}y(\text{C})\ensuremath{\le}0.3$ ranges of concentrations. We found that both dopants reduce the number of holes in the $\ensuremath{\sigma}$ bands until they are completely filled at different doping concentrations. These concentrations correlate with the experimentally observed loss of superconductivity in these alloys. The largest influence of doping on the phonon dispersion is found for the branches connected to the ${E}_{2g}$ and ${B}_{1g}$ modes. For both alloys a sudden increase in the ${E}_{2g}$-mode frequency is observed in a well-defined region of Al and C concentrations which correlates with the topological changes in the $\ensuremath{\sigma}$-Fermi surfaces.