Rationally designing hierarchical micro-/nano-structures as anode materials is crucial for enhancing electrochemical performances for energy storage devices such as lithium- and sodium-ion batteries (LIBs and SIBs). Herein, Co9S8 nanoparticles encapsulated within carbon-nanoparticles-decorated N-doped carbon spheres (Co9S8-NC@C) are prepared as anode materials for efficient lithium and sodium storage. The Co9S8-NC@C composite is derived by pyrolyzing a mixture of commercially available acid yellow 49 and metanilic anion-intercalated Co(OH)2 on which is decorated by ZIF-67. The composite is endowed with the advantageous features: well-dispersed Co9S8 nanoparticle, the highly conductive 3D carbon skeleton and carbon decoration, as well as the appropriate specific surface area and micro-/mesopore size distribution. The Co9S8-NC@C anode nanomaterials exhibits a highly reversible capacity of 760 mAh g−1 after 100 cycles at 100 mA g−1, and an exceptional rate capability when used as LIBs anode nanomaterials, and a decent cycling stability up to 382 mAh g−1 after 100 cycles at 100 mA g−1 when used as SIBs anode nanomaterials, demonstrating highly enhanced electrochemical performances for LIBs and SIBs compared with those counterparts of the Co9S8-NC without the decoration of ZIF-derived carbon, Co9S8, and N-doped carbon spheres. The results can promise the preparation of 3D carbon spheres encapsulated transition-metal compounds as anode nanomaterials for electrochemical energy storage.