How are the first mammary glands thought to have functioned?
the production of liquids to nourish offspring has evolved separately in several lineages, mammals have simply taken it further than any other group. it is pretty easy to see how it could evolve, any way of providing even trace nutrients or nourishment to young will have an advantage in organisms that are already engaged in child rearing. it means exposing the young to predators less often. 

Mammary glands are modified sweat glands. 

Monotremes do not have nipples what they have is a cluster of mammary glands on a patch of skin. the young have to suck the milk of the a cluster of hair. 

The exact sequence of how lactation evolved is not well known since lactation leaves no fossil evidence. 



Sources:

Lefèvre, C.M., Sharp, J.A. and Nicholas, K.R., 2010. Evolution of lactation: ancient origin and extreme adaptations of the lactation system. Annual review of genomics and human genetics, 11, pp.219-238.

Capuco, A.V. and Akers, R.M., 2009. The origin and evolution of lactation. Journal of Biology, 8(4), p.37.