There were 21 balls left on the final day when Zimbabwe bowled Afghanistan out to secure a 57-run win and a 1-0 victory in the two-match Test series in Sharjah.
It is Zimbabwe's first Test series win on foreign soil in 25 years, since succeeding in Bangladesh.
Chasing an improbable 417 to win were Afghanistan 5-143 overnight and seemingly heading for a heavy defeat.
But Maroofkhil, primarily a spin bowler, batted for nearly six hours for his 101 not out, scoring the first century of his senior career despite pulling his hamstring in the closing overs.
The 20-year-old became the first player to get a five-wicket haul and a century on his Test debut since New Zealand's Bruce Taylor in 1965, having also taken seven wickets in Zimbabwe's first-innings 339.
Afghanistan scored 217 in response, and Zimbabwe declared their second innings on 9-295.
Zimbabwe looked close to victory with Afghanistan struggling at 8-299, before Maroofkhil and Mohammad Saleem (22) added 59 runs for the ninth wicket as the light faded in Sharjah.
But Brad Evans (3-55) trapped Saleem lbw before Graeme Cremer got Ziaur Rahman out for a duck to seal victory for Zimbabwe.
Hosts Afghanistan staged the series in the United Arab Emirates for security reasons.