I think I can prove that if X=[0,1] (or 1-dimensional) then the claim of FelipeG is true.
Indeed, let $X=[0,1]$ and $D,f$ be as above, and proceed by contradiction. Assume there are points $a,b\in [0,1]$ such that $a<b$ and $f(a)=f(b)$; then, since $(a,b)\cap D$ has infinitely many elements, then there is some point $c\in D \cap (a,b)$ such that $f(c)=s \neq r$ (say $s>r$). On the other hand, by continuity of $f$, for every value $y\in D\cap (r,s)$ (which is not empty) there is an element $x_1\in (a,c)$ such that $ f(x_1) = y $ and an element $x_2\in (c,b)$ such that $ f(x_2)= y $. But, since $f^{-1}D=D$ both $x_1, x_2$ belong to $D$, which contradits the injectivity of $f_{|_D}$.
PS: this construction works if $f_{|_D}$ is a bijection (as one might argue from the title), otherwise there is a counterexamplecounterexample