Relativistic heavy-ion experiments have observed similar quenching effects for (prompt) $D$ mesons compared to charged hadrons for transverse momenta larger than 6-8~GeV, which remains a mystery since heavy quarks typically lose less energies in quark-gluon plasma than light quarks and gluons. Recent measurements of the nuclear modification factors of $B$ mesons and $B$-decayed $D$ mesons by the CMS Collaboration provide a unique opportunity to study the flavor hierarchy of jet quenching. Using a linear Boltzmann transport model combined with hydrodynamics simulation, we study the energy loss and nuclear modification for heavy and light flavor jets in high-energy nuclear collisions. By consistently taking into account both quark and gluon contributions to light and heavy flavor hadron productions within a next-to-leading order perturbative QCD framework, we obtain, for the first time, a satisfactory description of the experimental data on the nuclear modification factors for charged hadrons, $D$ mesons, $B$ mesons and $B$-decayed $D$ mesons simultaneously over a wide range of transverse momenta (8-300~GeV). This presents a solid solution to the flavor puzzle of jet quenching and constitutes a significant step towards the precision study of jet-medium interaction. Our study predicts that at transverse momenta larger than 30-40~GeV, $B$ mesons also exhibit similar suppression effects to charged hadrons and $D$ mesons, which may be tested by future measurements.