Let $f:Y \rightarrow X $ be a flat morphism of locally ringed space, i.e. the canonical map $f^\#:\mathcal{O}_{X,f(p)}\rightarrow \mathcal{O}_{Y,p}$ is a flat ring map for every $p \in Y$.
Then we can consider for $U \subset X$ open the map of locally ringed spaces $f_{|f^{-1}(U)}:f^{-1}(U) \rightarrow U$. I thought that the restriction will also be flat as we have for every $p \in f^{-1}(U)$ the identities $\mathcal{O}_{f^{-1} (U),p}\cong \mathcal{O}_{Y,p}$ and $\mathcal{O}_{U,f(p)}\cong \mathcal{O}_{X,f(p)}$. Hence $f_{|f^{-1}(U)}^\#:\mathcal{O}_{U,f(p)}\rightarrow \mathcal{O}_{f^{-1} (U),p}$ is flat as $f^\#:\mathcal{O}_{X,f(p)}\rightarrow \mathcal{O}_{Y,p}$ is flat for every $p \in f^{-1}(U)$.
But then I found this discussion Restriction of flat morphism and got confused. So what is the mistake in the argumentation above?
Everything is correct in your argument. In fact flat morphisms of schemes are stable under base change.
In the linked question the OP restricted also the domain of a flat morphism to a closed subscheme and this makes it completely different from the situation you are considering.