The profinite topology on any abstract group $G$, is one such that the fundamental system of neighborhoods of the identity is given by all its subgroups of finite index. We say that a group $G$ has the Ribes-Zalesskii property of rank $k$, or is RZ$_{k}$ with $k$ a natural number, if any product $H_{1} H_{2} \cdots H_{k}$ of finitely generated subgroups $H_{1}, H_{2}, \cdots , H_{k}$ is closed in the profinite topology on $G$. And a group is said to have the Ribes-Zalesskii property or is RZ if it is RZ$_{k}$ for any natural number $k$. In this paper we characterize groups which are RZ$_{2}$. Consequently, we obtain condition under which a free product with amalgamation of two RZ$_{2}$ groups is RZ$_{2}$. After observing that the Baumslag-Solitar groups $BS (m, n)$ are RZ$_{2}$ and clearly RZ if $m= n$, we establish some suitable properties on the RZ$_{2}$ property for the case when $m= -n$. Finally, since any group $BS (m, n)$ can be viewed as a HNN-extension, then we point out the Ribes-Zalesskii property of rank two on some HNN-extensions.