Render a model into a render target preserving the z-order

  3d, c++, directx, directx-9

Is there a way to render a model into a separate render target while preserving its z-order, so the parts of the model that are hidden behind a wall are not rendered?
Here is a visualization of what i want to achieve:

How it looks on the screen

How it looks in the rendertarget

Source: Windows Questions C++