E = mc^2 specifically applies only to objects that have nonzero mass and are at rest with respect to the observer. Photons are massless and move at the speed of light.
The general equation is E = sqrt((mc^2)^2 + (pc)^2) for rest mass m and momentum p. If a particle has mass and is at rest, then p=0 so E=mc^2. If a particle is massless, then m=0 so E=pc.
(The "m" here refers to rest mass m0, not the "relativistic mass" m* which is defined as m* = m0 / sqrt(1-(vc)^2)). Relativistic mass is best thought of as a fake concept to hide the ugly sqrt denominator. People can imagine things getting heavier when they're moving, and can keep saying "Einstein discovered E=mc^2". But it still has division-by-zero problems with massless particles, and things don't really "get heavier" when they move, so if you try to avoid thinking in terms of m* you won't get as confused. Neither m nor m* makes E=mc^2 work with photons.
Imagine if a bundle of photons could gather and form a "black hole". The hole and its event horizon would be constrained to move at the speed of light, which you can't, since you have mass. so you might easily escape its event horizon- you wouldn't have time to fall in before the thing was gone. Real black holes have mass and don't move at the speed of light relative to anybody.