Pakistan ended a 16-year wait for a Hockey World Cup victory after defeating Japan 4-3 in a classification match in the Netherlands.