Armand Laurienté scored twice for Sassuolo, putting the hosts ahead in the 25th minute and restoring parity in the 65th, but Alberto Aquilani's side were unable to prevent Fiorentina from taking all three points.
Pellegrino equalised in first-half stoppage time, heading home after Alieu Njie's contribution, before the latter put Fiorentina ahead five minutes into the second half.
Laurienté made it 2-2, but substitute Pedro Gonçalves helped create the opening for Mastantuono to restore Fiorentina's lead in the 79th minute. Víctor Valdepeñas added a fourth in stoppage time.
Fiorentina coach Paolo Vanoli was particularly pleased with the contribution of his substitutes, believing the collective response was an important step towards becoming a more cohesive team.
"I want to congratulate those who came on because from Gnonto to Pedro, Valdepeñas and Beto, they responded when called upon," Vanoli said.
"That is what makes things difficult for the coach, but it also helps us become a team. For three matches we had been very close to getting a victory, and this win must give us great confidence."
Vanoli acknowledged that the first half had been disappointing, with both sides appearing nervous and struggling to take control.
"I agree about the first half. I expected it to be like that in some ways, with two teams who were afraid and looking for a result. With a young team, it is sometimes difficult to recover mentally, but they were good at staying in the match."
He was also encouraged by the way Fiorentina responded despite the problems they have experienced in recent weeks.
"We have shown on the pitch that it is possible. The problem is how we do it. They are all starters, and tonight they demonstrated that. We are Fiorentina and we have to take Fiorentina higher."
However, Vanoli warned that there was still plenty of work to do, particularly defensively.
"There is still a lot to work on because we cannot concede two goals tonight. We have to understand the balance of the team, in both phases of the game."
Aquilani was left frustrated by another defensive collapse, insisting his team continued to make mistakes which made it difficult to recover once matches turned against them.
"I agree with what you said. The feeling is exactly that: when a match goes down a path where you are not comfortable, anything can happen," the Sassuolo coach said.
"We made some changes and conceded goals with alarming ease. We were unable to correct it. We make too many mistakes, and that is what I don't like. It annoys me."
The coach admitted that his side were mentally fragile, particularly when confronted with setbacks.
"I see a very fragile team, a team that struggles when it moves outside its own comfort zone. As soon as something negative happens, we go down too much, lose quality, make too many mistakes and stop having an impact."
Aquilani also accepted responsibility for the situation.
"I take all the responsibility. You cannot concede four goals in a match. We have to face reality, and we have to do it quickly, otherwise this is going to become a long story."
Vanoli, meanwhile, was keen to use the victory as a foundation for further improvement rather than regard it as a solution to Fiorentina's problems.
"The team intrigues me. I will do my best to find the right shape to win, not just to entertain. We are here to win, and we have to be mentally prepared for that."
