Does light loses its energy when it passes through denser medium?
$E=mc^2$ is not really applicable to light. It is applicable to something that has mass.
The energy of light is given by $E=h\nu$ where $\nu$ is the frequency of light and $h$ is Planck's constant, which has a value of $\approx 6.626 \times 10^{-34} J.s$
When light enters a different medium, its frequency remains the same, and of course, so does Planck's constant. Hence, obviously, its energy remains the same throughout the entire exercise.
To a first approximation, the light is still travelling at the speed of light inside of the medium. There is an apparent slowdown because, as the light ray interacts with the molecules of the medium, it diffracts, causing it to change direction randomly. This then causes its travel length to increase, which makes the speed appear to be smaller, from a macroscopic perspective.
The velocity of the light's travel does not decrease, however.
I would also state that $E=mc^{2}$ is not a valid formula for light, as that formula is only valid for stationary objects. The appropriate version of this for light would be $E^{2} = c^{2}|{\vec p}|^{2}$, and you could answer your above question by also making an appeal to conservation of momentum.
To touch on and finalise Jerry Schirmer's answer: the "light" in a medium is not only "light" in the ordinary, vacuum sense of the word, it is a quantum superpositon of free photons and excited matter states. A photon travelling though a medium is repeatedly undergoes the following cycle: it is fleetingly absorbed by electrons in the medium, which re-emit a new photon in its place a fantastically short time afterwards (femtoseconds or less). The process is somewhat like fluorescence, aside from that energy, momentum and angular momentum are wholly transferred to the new photon, whereas in fluorescence, energy (as betokened by the Stokes shift), momentum and angular momentum (as betokened by direction and polarisation shifts) are all transferred to the medium. The delay arising from the absorption / re-emission is what makes the light seem to propagate slowly, but you can see that no energy is lost. A slight variation on this theme is the birefringent material, where energy and momentum are wholly returned to the re-emitted photon, but some angular momentum is exchanged and light thus exerts a torque on a birefringent medium: see the second and third sections of my answer here and indeed there is a classic experiment demonstrating light's angular momentum by R. Beth, "Mechanical Detection and Measurement of the Angular Momentum of Light", Phys. Rev. 50 1936 pp115-127. But energy can still in principle conserved: in practice some mediums have attenuations, but some are fantastically small, for example, silica in the optical telecommunications window between $1350nm$ and $1550nm$ and, for the purposes of this argument, attenuations can in principle be nought.
Witness here is that the Poynting vector in the medium is the same as its freespace value: $\vec{S} = \vec{E}\wedge \vec{H}$, whereas the energy density $U = \frac{1}{2}\vec{D}\cdot\vec{E} + \frac{1}{2} \vec{B}\cdot\vec{H}$ in the medium is now higher: this is simply analogous to the steady state behaviour of a water tank with inlet and outlet pipe: transiently the water output rate can be less than that at the input whilst the tank fills up, but at steady state the two rates must balance. Likewise for the medium: the higher energy densities represent increased energy stores in the matter of the medium owing to the excited matter state parts of the total quantum superposition (there are also reflected energies at the medium's input and output which must be accounted for in an exact descripition, but the essential gist of this paragraph does not change).