Comeback denied: Niners outlast Pats