Given, $AB = B$ multiply by A on both sides $ABA=BA \, \, \, \, \, \, \, \, \, \, $ .....(i)
Also, $BA= A$
Multiply by $B$ on both sides $BAB= AB \, \, \, \, \, \, $....(ii)
Adding (i) and (ii), we get $ABA + BAB = BA + AB $
$\Rightarrow A(BA) + B(AB) = BA + AB$
$\Rightarrow AA + BB= A + B$
$\Rightarrow A^2+ B^2 = A + B$