Let S be either a free group or the fundamental group of a closed hyperbolic surface. We show that if G is a finitely generated residually-p group with the same pro-p completion as S, then two-generated subgroups of G are free. This generalises (and gives a new proof of) the analogous result of Baumslag for parafree groups. Our argument relies on the following new ingredient: if G is a residually-(torsion-free nilpotent) group and $$H\le G$$
H
≤
G
is a virtually polycyclic subgroup, then H is nilpotent and the pro-p topology of G induces on H its full pro-p topology. Then we study applications to profinite rigidity. Remeslennikov conjectured that a finitely generated residually finite G with profinite completion $${\hat{G}}\cong {\hat{S}}$$
G
^
≅
S
^
is necessarily $$G\cong S$$
G
≅
S
. We confirm this when G belongs to a class of groups $${\mathcal {H}_\textbf{ab}}$$
H
ab
that has a finite abelian hierarchy starting with finitely generated residually free groups. This strengthens a previous result of Wilton that relies on the hyperbolicity assumption. Lastly, we prove that the group $$S\times \mathbb {Z}^n$$
S
×
Z
n
is profinitely rigid within finitely generated residually free groups.