We present a method for obtaining well-localized Wannier-like functions (WFs) for energy bands that are attached to or mixed with other bands. The present scheme removes the limitation of the usual maximally-localized WFs method (N. Marzari and D. Vanderbilt, Phys. Rev. B 56, 12847 (1997)) that the bands of interest should form an isolated group, separated by gaps from higher and lower bands everywhere in the Brillouin zone. An energy window encompassing N bands of interest is specified by the user, and the algorithm then proceeds to disentangle these from the remaining bands inside the window by filtering out an optimally connected N-dimensional subspace. This is achieved by minimizing a functional that measures the subspace dispersion across the Brillouin zone. The maximally-localized WFs for the optimal subspace are then obtained via the algorithm of Marzari and Vanderbilt. The method, which functions as a postprocessing step using the output of conventional electronic-structure codes, is applied to the s and d bands of copper, and to the valence and low-lying conduction bands of silicon. For the low-lying nearly-free-electron bands of copper we find WFs which are centered at the tetrahedral interstitial sites, suggesting an alternative tight-binding parametrization.