If anyone is having problems with the new version of Unity HDRP (6000.0.34f1), I found a solution for this.
I used the same MaskStencil shader for masking, but when you use a transparent material for a surface that will be masked you ll see lots of overlapping and z-fighting. While I was playing around with the transparent material, I found a feature that gave me exactly what I wanted. Transparent Depth Prepass
Hope it helps Final