Section 39C. A town may construct and maintain on the lands acquired and held by it under section thirty-nine B proper dams, wells, reservoirs, pumping and filtration plants, buildings, standpipes, tanks, fixtures and other structures, including also purification and treatment works, the construction and maintenance of which shall be subject to the approval of the department of environmental protection, and may make excavations, procure and operate machinery, and provide such other means and appliances and do such other things as may be necessary for the establishment and maintenance of complete and effective water works; and for that purpose may construct, lay and maintain aqueducts, conduits, pipes and other works, under or over any lands, water courses, railroads, railways and public or other ways, and along any such way in said town in such manner as not unnecessarily to obstruct the same; and for the purposes of constructing, laying, maintaining, operating and repairing such conduits, pipes and other works, and for all other proper purposes of sections thirty-nine A to thirty-nine E, inclusive, a town may dig up or raise and embank any such lands, highways or other ways in such manner as to cause the least hindrance to public travel thereon; provided, that all things done upon any such way shall be subject to the direction of the selectmen of the town or, in the case of a state highway, of the state department of highways; and provided, further, that no such town shall enter upon, or construct or lay any conduits, pipes or other works within, the location of any railroad corporation except at such time and in such manner as it may agree upon with such corporation or, in case of failure so to agree, as may be approved by the department of telecommunications and energy.