The answer is 'yes'. Furthermore, this is more or less equivalent to a group-theoretic fact, which applies in much greater geneality, called Shenitzer's Lemma.
First, note that we may assume that $X$ and $Y$ are irreducible, since a standard innermost disc argument shows that any compressing disc can be made disjoint from any essential sphere.
SinceWe can now translate into group theory using the boundaries offollowing lemma. Recall that, by Stallings' Ends Theorem, a finitely generated group $X$$G$ is one-ended if and only if $Y$ are incompressible, it follows from Dehn's lemma$G$ is infinite and the Sphere theorem that their fundamental groups are onedoes not split non-endedtrivially (as an amalgamated product or HNN extension) over a finite subgroup.
Lemma: Let $M$ be a compact, orientable 3-manifold with non-empty boundary and infinite $\pi_1M$. Then $\pi_1M$ is one-ended if and only if $\partial M$ is incompressible.
NowProof: If $\partial M$ is compressible, then cutting along the compressing disc realizes a splitting of $\pi_1M$ over the trivial subgroup. Conversely, if $\pi_1M$ splits over a finite subgroup, then by a standard argument of Stallings--Epstein--Waldhausen, the splitting can be realized by cutting $M$ along some properly embedded, essential surface $\Sigma$. Since $\pi_1\Sigma$ is finite, $\Sigma$ is either a disc or a 2-sphere; but since $M$ is irreducible, $\Sigma$ must be a disc, and the boundary is incompressible. QED
Since we may assume that $X$ and $Y$ are irreducible, and since $\pi_1X$ and $\pi_1Y$ are infinite, the answer now follows immediately from Shenitzer's Lemma, a very useful fact in group theory which explains exactly when an amalgam over (see$\mathbb{Z}$ is one-ended. See, for instance, Theorem 18 of https://arxiv.org/abs/1102.2866v2here), it follows that the fundamental groupor this paper of Touikan for a comprehensive modern treatment.
In fact, you only need the glued manifoldfollowing weaker fact, which can be proved directly using Bass--Serre theory.
Weak Shenitzer's Lemma: If $G=A*_{\mathbb{Z}} B$ and $A,B$ are one-ended then so is $G$.
Proof: It's enough to show that $G$ does not split non-trivially over a finite subgroup. Suppose therefore there is alsosuch a non-trivial splitting, and let $T$ be the corresponding Bass--Serre tree. Since $A$ and $B$ are one-ended and infinite, they each fix unique vertices $a$,$b$. In particular But then $\mathbb{Z}$ fixes every edge on the geodesic between $a$ and $b$, so since edge stabilizers are finite, $a=b$. Therefore this is a global fixed point for $G$, and the splitting was trivial. QED
The full force of Shenizer's Lemma would give you the slightly stronger statement that it's sufficient to have every compressing disc crossed by your closed curve on the boundary.
The proof of Shenitzer's lemma is incompressiblebasically a group-theoretic version of the kind of geometric argument that @ThiKu outlines in the other answer.