In matrix notation, the fitted values can be written as $\hat y=Py$, with the projection matrix $P=X(X'X)^{-1}X'$, wich can be verified by plugging in the definition of the OLS estimator into the formula for the fitted values, $\hat y =X\hat\beta$.
Their mean is, with $\iota$ a vector of ones,
$$
\iota'Py/n,
$$
as the inner product with $\iota$ just sums up elements, $\iota'a=\sum_ia_i$.
In general, we have $PX=X$, as can be verified by direct multiplication.
Now, if $X$ contains $\iota$, i.e., if you have a constant in your regression, we have $P\iota=\iota$, as one of the columns of the result $PX=X$.
Hence, by symmetry of $P$ (which, again, can be verified directly),
$$
\iota'Py/n=\iota'y/n,
$$
the mean of $y$. Hence, the statement is true if we have a constant in our regression. It is - see the comment by @jld - however also true if there are columns of $X$ that can be combined into $\iota$. That would for example be the case if we have exhaustive dummy variables but no constant (to avoid the dummy variable trap).
A little numerical illustration:
y <- rnorm(20)
x <- rnorm(20)
lm_with_cst <- lm(y~x)
mean(y)
mean(fitted(lm_with_cst))
lm_without_cst <- lm(y~x-1)
mean(fitted(lm_without_cst))
Output:
> mean(y)
[1] 0.04139399
> mean(fitted(lm_with_cst))
[1] 0.04139399
> mean(fitted(lm_without_cst))
[1] 0.05660456