Stefany Shaheen, whose mother is retiring from the Senate, edged out veteran Maura Sullivan in a bruising primary, defying sparse polling that had her down in the competitive contest.