Let us take a condition,
Suppose, in this picture your friend is standing with a special torch (which can emit light from either two sides) in space and you are watching your friend from a distance. Your reference frame is S and your friend is at rest in S reference frame. Now, your friend switches on the torch for once and instantly switches off the torch. So the torch emits two same light pulses from either two sides. Suppose, the frequency of light is $\nu$ in S frame.
So, the energy of each light pulse is $e = h\nu$ [$h$ is plank constant]
Total energy lost is $E = 2e$
Your friend measured the energy before emission was $E_0$
Energy remained after emission is $E_1$
So the relation is, $E_1 = E_0 - E$
Now let's take another condition,
Here, you are moving towards right in a rocket with velocity $v$ where your friend is standing.
Let's name this reference frame S'. In this condition, you will see your friend moving towards left with velocity $v$ where you are inside the rocket as for relative motion.
In this reference frame your friend does the same. He switches on the torch for once and instantly switches off that torch. Now, applying relativistic doppler effect on both light pulses we get, the frequency of the left light pulse is $\nu\cdot\sqrt{\frac {c+v }{c-v }}$, with $c$ the speed of light.
and the frequency of right light pulse is $\nu\cdot\sqrt{\frac {c-v }{c+v }}$.
Multiplying the frequency by plank constant, we get the energy of the light pulses.
So, total energy lost in S'
\begin{align} &= h\nu\cdot \frac {\sqrt{c+v} }{\sqrt{c-v} }+h\nu\cdot \frac {\sqrt{c-v} }{\sqrt{c+v} }\\ &=e\left[ \frac {\sqrt{c+v} }{\sqrt{c-v} }+\frac {\sqrt{c-v} }{\sqrt{c+v} }\right] & (\text{since, } e = h\nu)\\ &=e\left[\frac{c+v+c-v}{\sqrt{c+v}\sqrt{c-v}}\right]\\ &=e\frac{2c}{\sqrt{c^2-v^2}}\\ &=2e\frac{1}{\sqrt{1-\frac{v^2}{c^2}}}\\ &=E\frac{1}{\sqrt{1-\frac{v^2}{c^2}}} & (\text{since, } E = 2e) \end{align}
Suppose, in S' reference frame energy before emission was $H_0$
and energy after emission is $H_1$
So the relation is, $H_1 = H_0 - E\frac {1}{\sqrt{1-\frac {v^2}{c^2}}}$
We can expand this expression using a Taylor series, $$(1-x)^{-1/2}\approx 1 + \frac {1}{2}x+\frac 3 8 x^2+\frac 5 {16} x^3+\dots$$
So, $$\frac{1}{\sqrt{1-\frac{v^2}{c^2}}}\approx 1 + \frac {1}{2}\frac{v^2}{c^2}+\frac 3 8 \frac{v^4}{c^4}+\frac 5 {16} \frac{v^6}{c^6}+\dots$$
If $v\ll c$ we can neglect terms of order higher than $v^2/c^2$.
And so, we can write $H_1 = H_0 - E\frac {1}{\sqrt{1-\frac {v^2}{c^2}}}$ —this equation as $H_1 \approx H_0 - E\left(1 + \frac {1}{2}\frac {v^2}{c^2}\right)$
Now, let's find out the difference of energy of that torch between S and S' reference frame.
The only difference of energy between the frames is kinetic energy. In reference frame S the torch is at rest and in S' frame the torch has kinetic energy. Let's assume the kinetic energy before emission in S' frame was $K_0$
So, $H_0 - E_0 = K_0 + B$ [$B$ is constant] — (i)
We know after emission the torch loses energy. Because kinetic energy is changing, we assume the kinetic energy after emission is $K_1$
So, $H_1 - E_1 = K_1 + B$ — (ii)
As velocity isn't changing in S' frame, we can say mass is changing. Because, kinetic energy depends on mass and velocity. Suppose, the mass before emission was $m_0$ and mass after emission is $m_1$.
Subtracting eq.(i) from eq.(ii) we get,
$H_0 - E_0 - H_1 + E_1 = K_0 + B - K_1 - B$
Or, $H_0 - E_0 - H_0 + E\left(1 + \frac {1}{2}\frac {v^2}{c^2}\right) + E_0 - E = K_0 - K_1$
Or, $E\left(1 + \frac {1}{2}\frac {v^2}{c^2}\right) - E =\frac {1}{2}m_0v^2- \frac {1}{2}m_1v^2$
Or, $E\left(1 + \frac {1}{2}\frac {v^2}{c^2} - 1\right) =\frac {1}{2}v^2(m_0 - m_1) $
Or, $\frac {1}{2}v^{2} \frac {E}{c^{2}}= \frac {1}{2} v^{2}m\ $ [let's take $m_0 - m_1 \equiv m$]
Or, $\frac {E}{c^{2}} = m\ $
Or, $\boxed{E = mc^2}$
This is how Einstein derivedN. B: $E = mc^2$This derivation has a more general form although the essence of it is present in this popular example