The key point is that the inclusion of $X$ into the mapping cylinder is a cofibration. It is a general fact that if $A \hookrightarrow B$ is a cofibration and a homotopy equivalence, then $A$ is a deformation retract of $B$. One way to think of this is to use the model structure on topological spaces (due to Strom) where (closed) Hurewicz cofibrations are cofibrations, Hurewicz fibrations are fibrations, and weak equivalences are homotopy equivalences.
So let's prove the following more general fact. Let $A \hookrightarrow B$ be an acyclic cofibration in a (closed) model category, where $B$ is fibrant. Then $B$ "deformation retracts" onto $A$. Let us assume that our model categories have functorial factorizations, so there is always a functorial cylinder object $A \times I$ for $A$.
To see this, let us first show that $B $ retracts onto $A$. By replacing the model category with the model category of objects under $A$, we can assume that $A$ is the initial object $\emptyset$, and $B$ is cofibrant in such a way that $\emptyset \to B$ is a weak equivalence.
This we can do by considering the lifting diagram with $\emptyset \to B$ and $\emptyset \to \ast$ (where $\ast$ is the final object).
We get a retraction $B \to \emptyset$.
Now we want a "deformation retraction." Let us consider the two maps $B \rightrightarrows B$ given by the identity and the retraction. We want a homotopy between the two.
But we can consider the lifting diagram with $B \sqcup B \to B \times I$ (for $B \times I$ a cylinder object) and $B \to \ast$. Since $\emptyset \to B$ is an acyclic cofibration, so is $\emptyset \to B \sqcup B$, and two-out-of-three shows that $\emptyset \to B \times I$ is a weak equivalence too; thus $B \sqcup B \sqcup \emptyset \times I \to B \times I$ is a trivial cofibration. Thus a lifting exists in the diagram, which is the map $B \times I \to B$ that we wanted.
Okay, here is my proof which took me a while to elaborate, but I think it's correct:
Note that $(D^n,S^{n-1})$ has the homotopy extension property (HEP). The salient point of my proof is a retraction of $B\times I\times I$ onto $(A\times I\times I)\ \cup\ (B\times\{0\}\times I)\ \cup\ (B\times I\times\{0\}),$ whenever $(B,A)$ has the HEP.
For $(s,t)\in I\times I$ let $s^*(s,t)=||(s,t)-d(s,t)||/||(1,1)-d(s,t)||$. Let $r$ be the retraction of $B\times I$ onto $A\times I\ \cup\ X\times\{0\}$ with coordinates $(r_x,r_s)$, and let $d:I\times I\ \longrightarrow\ I\times\{0\}\ \cup\ \{0\}\times I$ be a retraction. Now define $R:B\times I\times I\ \longrightarrow\ (A\times I\times I)\ \cup\ (B\times\{0\}\times I)\ \cup\ (B\times I\times\{0\})$,
$(x,s,t)\mapsto(r_x(x,s^*),\ \ d(s,t)+r_s(x,s^*)\cdot[(1,1)-d(s,t)])$. It is not difficult to prove that $R$ is a well-defined retraction.
Two spaces are homotopy equivalent iff they are strong deformation retracts of a larger space, and this larger space can be chosen to be the mapping cylinder $M(g)=Y\cup X\times I$, where $(x,1)\sim g(x)$. Then you can glue $B\times I$ in a canonical way, by the map $f\times\text{Id}_I$. There are maps $X\times0\xleftarrow{\ \ r\ \ }M(f)\xrightarrow{g\circ p_X}Y$ where $r$ is a deformation retraction, meaning there is a $k:M(f)\times I\to M(f),\ k(-,1)=r,\ k(-,0)=\text{Id}_{M(f)}$, a homotopy from $r$ to the identity. The map $g\circ p_X$ is a retraction homotopic to the identity via the map $h$ such that $h(x,s,t)=(x,t+s-ts)$.
Let us define a map $K:(M(f)\cup_{f\times Id} B\times I)\times I\ \longrightarrow\ M(f)\cup_{f\times Id}B\times I$ whose restriction onto $B\times I\times I$ is defined in the following way: We have shown that $B\times I\times I$ retracts onto $A\times I\times I\ \cup\ B\times(\{0\}\times I\cup I\times\{0\})$ A map on the first term is given by $k\circ(f\times\text{Id}_I\times\text{Id}_I)$, and one on the second term by $(b,s,t)\mapsto(b,s)$. They coincide on the common domain, and so they induce a map $K':B\times I\times I\to M(f)\cup_{f\times Id}B\times I$. We then combine $K'$ with $k$ on the cylinder to obtain $K$. One can then check that this is a deformation retraction onto $X\times\{0\}\cup_{f\times 0}B\times\{0\}$.
What is left is to find a deformation retraction onto $Y\cup_{gf}B$. But this one can even be explicitly written down, and the formula is practically the same as for $h$.
Best Answer
The textbook is wrong, and the answer you linked to is right. Here's an even simpler example. Let $X$ be an infinite discrete space, and let $A$ be $X$ minus one point. Then $X$ and $A$ are homeomorphic and therefore homotopy equivalent. Bur the inclusion map $i$ is not a homotopy equivalence. Indeed, if $r:X\to A$ is any map and if $p$ is the point in $X-A$, then $i(r(p))\neq p$ and there is no path from $i(r(p))$ to $p$. Therefore, there is no homotopy from $i\circ r$ to the identity map.