Why the energy of electromagnetic waves is directly proportional to frequency whilst for mechanical waves this is not true? The energy of a photon is directly proportional to its (angular) frequency:
$$
E=\hbar \omega.
$$
The energy of a classical mechanical wave is, however, proportional to the square of $\omega$:
$$
E=\frac{1}{2}\mu A^2\omega^2 \lambda
$$
per wavelength.
I struggle to see why there is $\omega$ in one equation but $\omega^2$ in another.
It is quite mysterious, though, because the $E$ in two equations mean different things. How many photons per wavelength do we have?
 A: The difference isn't electromagnetic versus mechanical. All classical waves behave the same way, as do all quanta. The difference arises because you're treating the light as quantum and the mechanical wave as classical.


*

*The energy of a single quantum is always proportional to $\omega$. This is true for photons, but it's also true for the quantized excitations that make up, say, classical waves on a string.

*For any wave described by a generalized coordinate $\phi(t)$ obeying the ideal wave equation and with standard normalization, the energy density of a classical plane wave at fixed amplitude is always proportional to $\omega^2$. This is true for both classical waves on a string (where the coordinate is the height $y(x, t)$) and for electromagnetic waves (where the coordinate is the vector potential $\mathbf{A}(\mathbf{x}, t)$).


Combining these two results shows that the density of quanta making up a classical ideal plane wave of fixed amplitude and frequency $\omega$ is proportional to $\omega$. 
A: i am not a physics major but substitute
$$
\lambda = \frac{c}{f}\,\, $$
in this equation
$$
E=\frac{1}{2}\mu A^2\omega^2 \lambda
 $$
will give you E as a function of frequency not frequency squared where C is a constant speed of light also we have the power of an EM wave not just one photon as E^2 and B^2 much similar to amplitude^2 in mechanical wave equation again
A: There is a subtle problem with the comparison you're making. $E = \hbar\omega$ is valid for only one photon/quanta. The correct formula is actually $E = \hbar\omega N$, where $N$ is the number of photons/quanta in question. 
This modifies your considerations, some. The classical formula for the energy of a sine wave on a string length $L$ is
$$E = \frac{1}{2}\mu \omega^2 A^2 L,$$
where $\mu$ is the linear density of the string, $A$ is the amplitude of the wave, and $\omega$ is the angular frequency. Now, such waves on strings fall into the realm of sound waves, as I understand it, and so could be said to be composed of phonons. So, we can drop in the quantum mechanic formula to get that
$$N = \frac{1}{2 \hbar}\mu \omega A^2 L.$$
So, it's not so much that the classical and quantum results disagree. In fact, they cannot disagree - the quantum result has to reduce to the classical one in the appropriate limit (see also: the correspondence principle and Ehrenfest's theorem). What's really going on is that the number of quanta that a classical wave contains is also proportional to $\omega$, and that's what eats the second factor of $\omega$.
A bunch more gory details and formulae follow.
For the simple harmonic oscillator (SHO) the energy above the ground state is given by
\begin{align}
  E & = \frac{p^2}{2m} + \frac{m\omega^2}{2} x^2 - \frac{\hbar\omega}{2} \\
    & = \hbar\omega n,
\end{align}
implying $n = \frac{p^2}{2m\hbar\omega} + \frac{m\omega}{2\hbar}x^2 - \frac{1}{2} = \frac{m\omega}{2\hbar} \left(x - \frac{i}{m\omega}p\right)\left(x + \frac{i}{m\omega}p\right)$ (remember that in QM $[x,p] = xp - px = i\hbar$).
For a real free scalar field that obeys the Klein-Gordon equation classically, you get that 
\begin{align}
   E &= \int \frac{c^2}{2}\pi^*(\mathbf{k})\pi(\mathbf{k}) + \frac{\omega^2}{2c^2}\phi^*(\mathbf{k})\phi(\mathbf{k}) - \frac{\hbar \omega}{2} \delta^{(3)}(0) \,\mathrm{d}^3k \\
     &= \int \hbar\omega \mathcal{N}(\mathbf{k})\,\mathrm{d}^3k,
\end{align}
where $\mathcal{N}(\mathbf{k})$ is the operator that gives the density of quanta/particles per cubic wave-number (we call it the density per mode), and $\omega = c\sqrt{k^2 + m^2c^2 / \hbar^2}$. Note also that $\pi^*(\mathbf{k})$ is the field momentum canonically conjugate to $\phi(\mathbf{k})$, so $[\phi(\mathbf{k}),\pi^*(\mathbf{k}')] = i\hbar \delta^{(3)}(\mathbf{k}-\mathbf{k}')$ (hence the delta function representing the vacuum energy). The complex conjugates are there because $\phi(\mathbf{x})$ is a real function, so its Fourier transform, $\phi(\mathbf{k})$, is complex. Like with the SHO we get 
\begin{align}
  \mathcal{N}(\mathbf{k}) &= \frac{c^2}{2 \hbar\omega}\pi^*(\mathbf{k})\pi(\mathbf{k}) + \frac{\omega}{2\hbar c^2}\phi^*(\mathbf{k})\phi(\mathbf{k}) - \frac{\delta^{(3)}(0)}{2} \\
  &= \frac{\omega}{4\hbar c^2}\left(\phi^*(\mathbf{k}) - \frac{ic^2}{\omega}\pi^*(\mathbf{k})\right)\left(\phi(\mathbf{k}) + \frac{ic^2}{\omega}\pi(\mathbf{k})\right) + (\text{same with }\mathbf{k}\rightarrow -\mathbf{k}).
\end{align}
Note how $\mathcal{N}$ is proportional to $\omega$. Keep in mind that the momentum amplitude for $\pi(\mathbf{k})$ is $\omega$ times the ordinary amplitude for $\phi(\mathbf{k})$, and that result will hold quantum mechanically, too (see: Heisenberg picture equations of motion).
All of that builds up to the electromagnetic field. The Electromagnetic field is considerably more complicated due to the need to fix a gauge and the vector calculus. The Energy is given by:
\begin{align}
  E & = \int \frac{\epsilon_0}{2} \mathbf{E}(\mathbf{k})\cdot\mathbf{E}(\mathbf{k}) + \frac{1}{2\mu_0} \mathbf{B}(\mathbf{k})\cdot\mathbf{B}(\mathbf{k})\,\mathrm{d}^3k \\
    & = \int \frac{\epsilon_0}{2} \mathbf{E}(\mathbf{k})\cdot\mathbf{E}(\mathbf{k}) + \frac{1}{2\mu_0} \left[\mathbf{k}\times \mathbf{A}(\mathbf{k})\right]\cdot\left[\mathbf{k}\times \mathbf{A}(\mathbf{k})\right]\,\mathrm{d}^3k.
\end{align}
Perhaps the simplest gauge to work in is the Weyl gauge, where the scalar potential is identically zero. Then you get that $\mathbf{E} = - \partial_0 \mathbf{A}$. The gauge fixing has removed one of the degrees of freedom. The remaining three can be broken down using Helmholtz decompositon in real space (in momentum space this amounts to breaking down a vector field into components parallel to $\mathbf{k}$ [radial/divergenceful] and perpendicular [tangential to the polar coordinate sphere/solenoidal]). 
Notice how $\mathbf{A}\cdot\hat{k}$, the divergenceful part of the vector potential, doesn't contribute to the energy (it had better not - this is the part of $\mathbf{A}$ that gauge transformations modify). The only contribution to the energy in the radial direction is the divergenceful part of $\mathbf{E}$, which is proportional to the negative of the momentum canonically conjugate to the divergenceful part of $\mathbf{A}$. In other words, that part of the field is 'free', and doesn't support quantized particle modes. Instead the modes are like the travelling wave modes we dealt with in the free Shcrödinger equation (i.e. when $H = p^2/(2m)$).
If we denote the solenoidal part of a vector field $\mathbf{A}(\mathbf{k})$ with $\mathbf{A}_\perp(\mathbf{k})$ and the divergenceful part with $A_{||}(\mathbf{k})$, then the energy can be written:
\begin{align}
   E & = \int \frac{\epsilon_0}{2}E_{||}^*(\mathbf{k}) E_{||}(\mathbf{k}) \,\mathrm{d}^3k \\
 & \hphantom{=} + \int \frac{\epsilon_0}{2} \mathbf{E}_\perp^*(\mathbf{k}) \cdot \mathbf{E}_\perp(\mathbf{k}) + \frac{1}{2\mu_0} k^2 \mathbf{A}_\perp^*(\mathbf{k}) \cdot \mathbf{A}_\perp(\mathbf{k}) \,\mathrm{d}^3k \\
 & = \int \frac{\epsilon_0}{2}E_{||}^*(\mathbf{k}) E_{||}(\mathbf{k}) \,\mathrm{d}^3k + \int \hbar\omega \mathcal{N}(\mathbf{k})\,\mathrm{d}^3 k.
\end{align}
The remaining trickiness is in terms of translating the symbols as used above. The dictionary is, roughly: $\pi \rightarrow -\sqrt{\epsilon_0} \mathbf{E}_\perp / c$ and $\phi \rightarrow \mathbf{A}_\perp / \sqrt{\mu_0}$. This gives:
\begin{align}
   \mathcal{N}(\mathbf{k}) & = \frac{\epsilon_0}{2\hbar \omega} \mathbf{E}_\perp^*(\mathbf{k}) \cdot \mathbf{E}_\perp(\mathbf{k}) + \frac{\omega}{2\hbar\mu_0 c^2} \mathbf{A}_\perp^*(\mathbf{k}) \cdot \mathbf{A}_\perp(\mathbf{k}) - \delta^{(3)}(0) \\
  & = \frac{\epsilon_0}{2\hbar \omega} \mathbf{E}_\perp^*(\mathbf{k}) \cdot \mathbf{E}_\perp(\mathbf{k}) + \frac{\omega \epsilon_0}{2\hbar} \mathbf{A}_\perp^*(\mathbf{k}) \cdot \mathbf{A}_\perp(\mathbf{k}) - \delta^{(3)}(0) \\
  & = \frac{\omega}{4\hbar c^2}\left(\frac{\mathbf{A}_\perp^*(\mathbf{k})}{\sqrt{\mu_0}} + \frac{ic \sqrt{\epsilon_0}}{\omega}\mathbf{E}_\perp^*(\mathbf{k})\right)\cdot\left(\frac{\mathbf{A}_\perp(\mathbf{k})}{\sqrt{\mu_0}} - \frac{ic \sqrt{\epsilon_0}}{\omega}\mathbf{E}_\perp(\mathbf{k})\right) \\
&\hphantom{=}+ (\text{same with }\mathbf{k}\rightarrow -\mathbf{k}) \\
  & = \frac{\omega \epsilon_0}{4\hbar }\left(\mathbf{A}_\perp^*(\mathbf{k}) + \frac{i}{\omega}\mathbf{E}_\perp^*(\mathbf{k})\right)\cdot\left(\mathbf{A}_\perp(\mathbf{k}) - \frac{i}{\omega}\mathbf{E}_\perp(\mathbf{k})\right) + (\text{same with }\mathbf{k}\rightarrow -\mathbf{k}).
\end{align}
Note that the loss of the $2$ in the denominator of $\delta^{(3)}(0)$ is not a mistake - there are two independent components in $\mathbf{A}_\perp$, and each one contributes a $\delta^{(3)}(0)/2$.
A: The relationship between energy and frequency is a classical result.  The only way I know of to derive it is using Special Relativity.  For a given light pulse, the momentum 4-vector transforms in such a way that the energy is inversely proportional to the wavelength.  
Basically, if you run away from the light, it catches up with you just as fast as if you were standing still, but it doesn't hit you as hard, and it's also red shifted.
I'm pretty sure that's how Einstein arrived at his theory of the photoelectric effect, thus introducing the photon.
The correlation between frequency and energy $E=h\nu$ is a statement about a single photon.  Not about EM waves. 
A: The energy associated to a "light quantum" (photon) is proportional to frecuency in the origin point, but weakens in linear way through the runned distance.  Furthermore, if the receptor of this photon is moving from emisor, the frecuency varies too. Therefore, the energy changes.
