49ers pull out victory, clinch 5th seed