Once a year, at the confluence of the Ganges and Yamuna rivers in northern India, something truly remarkable happens. During the lunar month of Magh, spanning January and February, a vast tented city comes into being. Pilgrims (kalpwasi) gather to bathe twice daily in the Ganges and live a life of spiritual devotion. Once every 12 years at the Kumbh (full) Mela (assembly), this arguably becomes the largest city on Earth. Many millions can attend the event on a single day.

The Mela seems to incorporate almost every imaginable form of urban blight. It is intensely crowded. It is noisy – competing loudspeakers blast out distorted sounds day and night. It is polluted – there are only the most rudimentary waste disposal facilities and sanitation. On top of that the kalpwasis live in flimsy tents with little or no heating at a time of year when temperatures regularly fall to near zero during the night and the devout rise before dawn to immerse themselves in the icy river.

All this might seem designed to create stress and irritation, to create tensions between people and to disrupt social functioning. This is certainly what most of the existing (western) literature would seem to suggest. And yet the Mela works. The annually reborn city not only functions, but functions smoothly. People interact harmoniously, they respect others, give space to others, show civility to others and provide overt help to others – even strangers.

We have been studying the Mela for six years now, using a range of methods from surveys to ethnographies to experiments, and it seems that – far from being stressed – people talk of their experience in powerfully positive terms. In one interview, conducted in shouts in order to be heard over the background noise, our kalpwasi described himself as "serene" and "blissful".

What is more, these effects seem to endure well beyond the event itself. The results of our survey have just been published in the journal PLoS One. They show that, compared with a control group of people not attending the festival, kalpwasis' wellbeing was better after the Mela than before. Despite the overwhelmingly bad press that crowds tend to receive, it seems they can be good for you.

How can this be? And what might it tell us, not just about one north Indian religious festival, but about the conditions of social life more generally?

Hindus at the Kumbh Mela on Monday. Pilgrims rise before dawn to bathe in the freezing Ganges. Photograph: Anindito Mukherjee/EPA

Our analysis suggests that the starting point lies in the development of a shared identity. Kalpwasis come to see themselves and others at the festival as a group: as "us" rather than "me" and "you".

On the one hand, this transforms relations between them. People cease to see their fellow kalpwasi as "other", but rather to share the same perspectives and goals. They expect to agree with others, to have their views validated by others and to receive support from others. All this makes social interaction smoother and social relations more rewarding.

On the other hand, the fact that kalpwasis share common goals, makes them more likely to understand and respect the perspective of others - and, crucially, to work together in order to achieve those goals. Kalpwasis give space for others to perform their devotions. They don't interrupt them or detain them with gossip. In this way – unlike everyday life where the constant distractions of work, neighbours and family inevitably drag one back to mundane, secular reality – the devout become better able to realise their fundamental goal of living a spiritual life.

Our rich ethnographic data, and the statistical modelling of our survey data, show that the combination of intimate social relations and the ability to live out the aspirations rooted in one's group identity (what we call "collective self-realisation") are critical to the extreme positivity that kalpwasis experience during the Mela.

At Kumbh Mela, pilgrims make space for others to perform their devotions. Photograph: Roberto Schmidt/AFP/Getty Images

Our analysis also shows it is the sense of intimate social relations – that we are not alone, that we can call on others, that these others form a "social safety net" for us – that creates improvements in wellbeing once kalpwasis leave the Ganges and go back to their everyday lives.

What our work demonstrates, then, is the power of identity in transforming collective experience and the power of collective experience in transforming everyday life. It shows how a sense of shared identity provides the underpinning for that sense of community and civility about which so much is spoken. And, perhaps most remarkably, it suggests that shared identity also improves our physical wellbeing, with symptoms of ill health less apparent after the Mela than before.

The Mela metropolis may only come into being for a month a year, but it has many effects that endure well beyond.

Stephen Reicher is a professor of psychology and neuroscience at the University of St Andrews. The overall findings of the Mela Research Programme will be presented at a press event on 24 January from 11-1pm at the Centre for Behavioural and Cognitive Sciences at the University of Allahabad in Uttar Pradesh, India. To attend, please contact Prof Reicher, Dr Nick Hopkins or Prof Narayanan Srinivasan