We completely describe the finitely generated pro-$p$ subgroups of the profinite completion of the fundamental group of an arbitrary $3$-manifold. We also prove a pro-$p$ analogue of the main theorem of Bass--Serre theory for finitely generated pro-$p$ groups.