The conservation of proton number and neutron number in a nuclear reaction only means the total count of protons and the total count of neutrons stays the same before and after, it says nothing about the total mass staying the same, because the mass of a bound nucleus depends on how tightly its nucleons are held together, not just on how many there are.
Step 1: Mass of a nucleus in terms of binding energy.
For any nucleus made of \( Z \) protons and \( N \) neutrons, its actual mass \( M \) is related to the binding energy \( E_b \) by \[M c^2 = \left( Z m_p + N m_n \right) c^2 - E_b\] that is, the mass of the bound nucleus is less than the mass of its separated, free nucleons by exactly \( E_b / c^2 \).
Step 2: Write a generic nuclear reaction.
Consider a reaction \( A + B \rightarrow C + D \), where the total number of protons and neutrons is the same on both sides, just redistributed among the nuclei. Using Step 1 for every nucleus involved, and noting the free-nucleon masses \( Z m_p + N m_n \) cancel exactly between the two sides because the same protons and neutrons are simply present in different nuclei, \[\left( M_A + M_B \right)c^2 - \left( M_C + M_D \right)c^2 = E_{b,C} + E_{b,D} - E_{b,A} - E_{b,B}\]
Step 3: Identify the energy released.
The left-hand side, \( \left[ (M_A + M_B) - (M_C + M_D) \right] c^2 \), is exactly the mass converted to energy in the reaction, called the Q-value: \[Q = \left[ (M_A + M_B) - (M_C + M_D) \right] c^2 = \Delta E_b\] where \( \Delta E_b = (E_{b,C} + E_{b,D}) - (E_{b,A} + E_{b,B}) \) is the increase in total binding energy from reactants to products.
Step 4: What this means.
If the products are more tightly bound than the reactants (\( \Delta E_b > 0 \), as happens in both fission of heavy nuclei and fusion of light nuclei, since both move the nucleons toward the peak of the binding-energy-per-nucleon curve near iron), then \( Q > 0 \): mass has decreased and that missing mass has appeared as released kinetic energy and radiation. If instead the reaction requires binding energy to be supplied (\( \Delta E_b < 0 \)), \( Q < 0 \), and mass increases at the expense of energy put in.
So mass converts to energy, or energy converts to mass, purely through the change in binding energy across the reaction, even while the number of protons and neutrons stays fixed throughout.