Sometimes, only the delocalisation of electrons is not sufficient to conclude that the compound is more stable. We should also look at which molecular orbital the electron pair is delocalising. But a clear picture of these kind of $\pi$-conjugated systems can be got when it is approached quantum-mechanically, with the help of Huckel-$\pi$ Molecular Orbital Theory.
According to this theory, to get the $\pi$-electronic energy of the system, we have to solve the Secular Determinant, which is $$\ce{det(H_{ij}- ES_{ij}) =0 ,where i,j varies from 1 to n}$$ in a $n$ Carbon System.
In Huckel Theory, for solving the determinants two approximations are taken,
$1$. $\ce{H_{ii} = $\alpha$ for all i, and H_{ij} = $\beta$ for |i -j|=1.}$ This approximation tells that the effective Hamiltonian for the whole conjugation is negligible if the carbons are not close to each other(i.e. adjacent).The two different values are obvious because of the in Hamiltonian, the Columb integral, the Exchange integral interferes.
$2$. $\ce{S_{ii} =1; and S_{ij}= 0 else}$. This tells the Overlap integral(Note that, this is an integral, and clear overlap in the picture doesn't mean integral should be large) are negligible for any two different carbons.
Taking these two approximations, if you solve the determinant, you will get energy levels for $5$ carbon systems,$$\ce{E = $\alpha$ + 2$\beta$; $\alpha$; $\alpha$ - $\beta$}$$Typically, the values of $\beta$ are negative and as you increase in $\alpha$ or decrease in $\beta$, the energy is increased and system gets destabilisation. .So, the lowest energy level is $\alpha$ + $2$$\beta$, and then the second and after that the third one.
Your first compound has four $\pi$ electrons. So, two electrons will be in the level $\alpha$ +$2$$\beta$, and other two is in $\alpha$ only. So,net energy, is $4$$\alpha$ + $4$$\beta$ in mostly conjugated system $1$st compound.
But in the second case there are only two $\pi$ electrons. That two will go into $\alpha$ + $2$$\beta$.So, net energy is $2$$\alpha$ + $4$$\beta$ in second one.
So, second system has lower energy than the first as coefficient of $\alpha$ is lesser in second one, though second compound is less conjugated. So, extent of delocalisation doesn't always determine stability.