A composition having nanoparticles of a refractory-metal carbide or refractory-metal nitride and a carbonaceous matrix. The composition is not in the form of a powder. A composition comprising a metal component and an organic component. The metal component is nanoparticles or particles of a refractory metal or a refractory-metal compound capable of decomposing into refractory metal nanoparticles. The organic component is an organic compound having a char yield of at least 60% by weight or a thermoset made from the organic compound. A method of combining particles of a refractory metal or a refractory-metal compound capable of reacting or decomposing into refractory-metal nanoparticles with an organic compound having a char yield of at least 60% by weight to form a precursor mixture.