Were they only cultivated for the upper class?
There are three main historical reasons for growing vegetables:
They can provide higher yields than alternatives (your alternatives being grains and grazing). Calorie density is far less important than calories produced per year per hectare. Depending on the local conditions - weather, climate, soils, water - some vegetable crops can greatly out-yield grains. Notable cases include potatoes in much of northern Europe, potatoes in Tibet (where the population tripled when the potato was introduced), sweet potatoes in China. Cassava and yams can be high-yielding in conditions where many grain crops do poorly. Especially in places where micro-local conditions don't suit grain (e.g., the land is too wet, or too sloped), vegetable crops can allow that land to be used.
Food security. Growing a diversity of crops provides insurance against crop failure. Different crops with different growing requirements and susceptible to different pests mean that some particular disease, pests, drought or flood is less likely to destroy one's entire crop. This is very important for subsistence farmers, for whom it is more important to maximise the minimum annual yield rather than to maximise the average yield - starving to death in a bad year is highly undesirable. Vegetables can be, and have been, grown in response to poor grain crops. It is in times of food scarcity that vegetables become more important (at least in places where the staple is a grain).
Flavour! For many subsistence farmers, meat is an occasional luxury. If their diet consisted of meat and grain, most of the time it would be just grain (bread, gruel/porridge, steamed/boiled grain). Vegetables can provide important and desirable variety, as side dishes to accompany the staple grain. Some vegetables have major roles in providing flavour in dishes as well as calories (alliums such as onion and garlic, and chillies, are notable for this). While you might disagree, many people believe that various vegetables taste better than plain bread or rice. A traditional simple meal in Korea would consist of grains (usually rice mixed with barley, or rice mixed with other grains), and a few vegetable side dishes (e.g., kimchi, simmered bean sprouts dressed with garlic and sesame, leafy green simmered and dressed with soy sauce and sesame) and was considered a much better and tastier meal than just the grains alone. Many people prefer a BLT to a plain bacon sandwich. Pasta with a tomato sauce to plain pasta.
An additional benefit is that vegetables can often be cooked more quickly and easily than grains (or even eaten raw, or pickled vegetables eaten without any further preparation). This can provides savings in time and fuel.
There is also the additional benefit that it isn't just calories that matter, but also micronutrients (e.g., vitamins). Vegetables can be an important source of micronutrients (and fibre).