The Massachusetts Bar Association has a nice piece on it here. Note that when last accessed, it referred to the law as Chapter 199, when it is actually 119.
The salient points are:
Since 1972, Massachusetts Law has provided for visitation rights to certain grandparents of unmarried children (who can visit their grandchildren):They are out of luck if the minor grandchildren are adopted by someone other than a stepparent.
If the parents of the child are divorced, married but living apart, or under a temporary order or judgment of separate support.
If either or both parents are deceased.
If the child was born out of wedlock and the paternity of the child has been established by a court or the father has signed an acknowledgment of paternity.
The full statute is available here. It is Massachusetts General Law Chapter 119, Section 39D. It includes the petition procedures for in and out-of-state grandparents.