This question already has an answer here:
We can get the Einstein field equations of GR from the Einstein-Hilbert action via two distinct methods:
First, by taking the metric as the only degree of freedom, and imposing right away that the connection is the Levi-Civita connection. In that case the Lagrangian will be a functional of the metric only since the Levi-Civita connection is expressible in terms of derivatives of the metric.
Second, we can follow the Palatini method: Take the metric and the connection as independent degrees of freedom and vary with respect to both. It happens curiously that the Euler-Lagrange equations for the connection will just be the condition that it must be the Levi-Civita connection.
It seems to be a wonderful coincidence that we get exactly the condition that the connection must be Levi-Civita one, thus recovering the Einstein field equations. So my question is: Why does this work? Is there any deeper mathematical reason why we get the right answer?