Milan went ahead after four minutes when Ante Rebic crossed from the left and Ibrahimovic, left unmarked, headed past Sampdoria goalkeeper Wladimiro Falcone who was making his Serie A debut at the age of 25.
However, Falcone made a good save to deny Ibrahimovic a second goal before halftime when the Swede's shot took a deflection.
Donnarumma did well to stop a Fabio Quagliarella free kick after the break before Hakan Calhanoglu added a second in the 54th minute. The referee initially disallowed it for a foul but the goal was awarded following a VAR review.
Calhanoglu also set up the third with a pass to Ibrahimovic who scored from a narrow angle four minutes later. It was the 38-year-old Swede's ninth league goal since he returned for a second stint with Milan in January.
"I'm like Benjamin Button, I've always been young, never old," Ibrahimovic told Italian TV, referring to a character played by Brad Pitt in a 2008 movie who aged in reverse.
With 50 league goals at AC Milan, Ibrahimovic becomes the first player to reach that mark for both San Siro clubs. He scored 57 goals for rivals Internazionale between 2006-09.
Milan stayed sixth with 63 points which will earn them a place in the Europa League second qualifying round next season.