I'd look at the answer of Haskell Curry on that post. Lebesgue measurability is unusual in that there are different $\sigma$-algebras on the domain and the codomain. For Lebesgue measurability, we are dealing with $f,g: (\mathbb{R},\sigma_{Lebesgue}) \to (\mathbb{R},\sigma_{Borel})$. Now, it is important to note that the $\sigma$-algebra of Lebesgue sets is strictly bigger than that of Borel sets. So if we start with $S \subset \mathbb{R}$ which is Borel, the Lebesgue-measurability of $g$ only guarantees that $g^{-1}(S)$ is Lebesgue-measurable, not necessarily Borel measurable, so one cannot use the Lebesgue-measurability of $f$ to conclude something about $f^{-1}(g^{-1}(S))$.
Less technical: to compose functions properly, you need to make sure the codomain of the first function is the domain of the second function. Since the codomain of $f$ is the same set as the domain of $g$, the functions can be composed pointwise. However, since the $\sigma$-algebras on the codomain of $f$ and domain of $g$ are not aligned, they cannot generally be composed as measurable functions. This is never a problem if you fix a $\sigma$-algebra on this space, which is what your textbook does.