Many animals have demonstrated abilities to count numbers - i.e. Numerical Cognition. This fits in with evolutionary theory in the sense that it could be advantageous for an animal to understand (small) numbers when involved in feeding, threat situations etc.
Number discrimination has now been demonstrated in mammals, birds, fish, reptiles and even insects etc. See ‘The origins of numerical abilities’ in Philosophical Transactions of the Royal Society B, Feb 2018.
There are as yet no neurological models to explain how this number-sense might operate.
In comparison to other animals, humans have advanced number perception abilities. Some researchers suggest that mathematical skills are primarily a cultural phenomenon :
“There is a widely accepted view in cognitive neuroscience, child psychology, and animal cognition that there is a biologically evolved capacity specific for number and arithmetic that humans share with other species.
However, data from various sources – humans from non-industrialized cultures, trained nonhuman animals in captivity, and the neuroscience of symbol processing in schooled participants – do not support this view.
The use of loose and misleading technical terminology in ‘numerical cognition’ has facilitated the elaboration of teleological claims which underlie the above view.
Biologically evolved preconditions for quantification do exist, but the emergence of number and arithmetic proper – absent in nonhuman animals – has materialized via cultural preoccupations and practices that are supported by language and symbolic reference – crucial dimensions that lie largely outside natural selection.”
Source: Is There Really an Evolved Capacity for Number? Trends in Cognitive Science, Volume 21, Issue 6, p409–424, June 2017
Nevertheless, some people seem to have extremely advanced mathematical skills from a very early age. There is yet no explanation as to why humans have evolved this advanced number processing ability. Which seems (in some individuals at least) to be innate.