First-principles full-potential linear muffin-tin orbital calculations have been used to study the 4d-transition-metal carbides ZrC, NbC, and MoC. The experimental phase diagrams at T=0 of the refractory compounds ZrC, NbC, and MoC have been reproduced with great accuracy from first principles theory. The energy of formation for these compounds has been calculated for several phases and stoichiometries in order to understand the differences in phase stabilities and the changes in homogeneity ranges found between these systems is explained. The results can be regarded as theoretical zero-temperature phase stability diagrams for the three compounds containing not only the experimentally verified but also hypothetical phases and many of the experimental properties and trends are reproduced and explained. A study of the changes and differences in electronic structure and bonding of the studied compounds, phases and stoichiometries is also presented. As a part of this study the hexagonal Me2C (Me being Zr, Nb, or Mo) phases were studied and the theoretical structures, with relaxed interlayer distances and lattice parameters, were obtained. The phase stabilities and electronic structure of the experimentally reported orthorhombic Nb2C and Mo2C phases were also studied.