Cauchy’s Integral Theorem says that if a function $f(z)$ is analytic throughout some simply connected domain $D$, then for any contour $C$ in $D$, $\oint_C f(z)dz=0$. It turns out that the converse of Cauchy’s Theorem is also true, namely
Theorem. If a function $f(z)$ is continuous in a simply connected domain $D$ and $\oint_C f(z)dz=0$ for every closed contour $C$ within $D$, then $f(z)$ is analytic throughout $D$.
This theorem is called Morera’s Theorem. Let us prove the theorem.
First we show that under the assumption the line integral $\int_\gamma f(z)dz$ is independent on the path $\gamma$. Let $\gamma$ be a path from $z_1$ to $z_2$ and choose $\gamma_1$ another path from $z_1$ to $z_2$ as shown in the figure.
Then $C: \gamma\cup (-\gamma_1)$ is a positively oriented contour. So, by assumption,
\begin{align*}
0&=\oint_C f(z)dz\\
&=\int_\gamma f(z)dz+\int_{-\gamma_1}f(z)dz\\
&=\int_\gamma f(z)dz-\int_{\gamma_1}f(z)dz.
\end{align*}
That is,
$$\int_\gamma f(z)dz=\int_{\gamma_1}f(z)dz.$$
Hence, the line integral $\int_\gamma f(z)dz$ does not depend on the path $\gamma$ but only on the endpoints $z_1$ and $z_2$.
Let us define $F(z)=\int_{z_0}^z f(w)dw$. Then
\begin{align*}
F(z+\Delta z)-F(z)&=\int_{z_0}^{z+\Delta z} f(w)dw-\int_{z_0}^z f(w)dw\\
&=\int_z^{z+\Delta z} f(w)dw.
\end{align*}
Since $\int_z^{z+\Delta z}dw=\Delta z$,
$$f(z)=\frac{1}{\Delta z}\int_z^{z+\Delta z}f(z) dw$$
and so, we have
$$\frac{F(z+\Delta z)-F(z)}{\Delta z}-f(z)=\frac{1}{\Delta z}\int_z^{z+\Delta z}[f(w)-f(z)]dw.$$
Since $f$ is continuous at $z$, given $\epsilon>0$ there exists $\delta>0$ such that $|f(w)-f(z)|<\epsilon$ whenever $|w-z|<\delta$. If $z+\Delta z$ is close enough to $z$ so that $|\Delta z|<\delta$, then
$$\left|\frac{F(z+\Delta z)-F(z)}{\Delta z}-f(z)\right|<\frac{1}{|\Delta z|}\epsilon|\Delta z|=\epsilon.$$
Therefore,
$$F'(z)=\lim_{\Delta z\to 0}\frac{F(z+\Delta z)-F(z)}{\Delta z}=f(z).$$
That is, $F(z)$ analytic in $D$. This means that $f(z)$ is analytic by Cauchy Integral Formula.