This fact is a Corollary of the many pointed version of the Seifert-van Kampen theorem, published in 1967.
If $X= U \cup V$ is path connected, where $U,V$ are open and path connected, and $U \cap V$ has $n$ path components, then the fundamental group of $X$ has the free group $F$ on $n-1$ generators as a retract. See Topology and Groupoids Section 8.4; see also this paper on the Phragmen-Brouwer Property, which gives a small correction to that section, and further reference to the literature. This paper proves by groupoid methods that if instead $U,V,W=U \cap V$ have $n_U,n_v,n_W$ path components, then the rank of the free group $F$ is $n_W-n_U-n_V+1$.