Starting point.
E=γmc2,p=γmv,γ=1−v2/c21
Computing E2−(pc)2.
E2−(pc)2=(γmc2)2−(γmvc)2=γ2m2c4−γ2m2v2c2
Factor out γ2m2c4:
E2−(pc)2=γ2m2c4(1−c2v2)
Using the definition of γ. Since γ2=1−v2/c21, the factor (1−v2/c2) exactly cancels γ2:
E2−(pc)2=γ2m2c4⋅γ21=m2c4
E2−(pc)2=(mc2)2
The right-hand side depends only on the mass m (and on c), not on the velocity: it is therefore the same in every inertial frame, i.e. an invariant.
Photon case (m=0). Setting m=0:
E2−(pc)2=0⇒E2=(pc)2⇒E=pc
The photon, having no mass, has energy and momentum related simply by E=pc and travels at speed c.