The efficient evolution of hydrogen through electrocatalysis is considered a promising approach to the production of clean hydrogen fuel. Platinum (Pt)-based materials are regarded as the most active hydrogen evolution reaction (HER) catalysts. However, the low abundance and high cost of Pt hinders the large-scale application of these catalysts. Active, inexpensive, and earth-abundant electrocatalysts to replace Pt-based materials would be highly beneficial to the production of cost-effective hydrogen energy. Herein, a novel organoimido-derivatized heteropolyoxometalate, Mo4-CNP, is designed as a precursor for electrocatalysts of the HER. It is demonstrated that the carbon, nitrogen, and phosphorus sources derived from the Mo4-CNP molecules lead to in situ confined carburization, phosphorization, and chemical doping on an atomic scale, thus forming nitrogen-doped porous molybdenum carbide and phosphide hybrids, which exhibit remarkable electrocatalytic activity for the HER. Such an organically functionalized polyoxometalate-assisted strategy described here provides a new perspective for the development of highly active non-noble metal electrocatalysts for hydrogen evolution.