Why are many fruits sour?
Short answer

Great question! I have done some research on the topic and found so much interesting information. It still seems like a topic that i being researched on, but so far, the research shows that malate is created by a CAM and C4 like pathway while citrate is created from TCA cycle.  Their functions are that they are important biomolecules in metabolism, in particular respiration.

Background

Malic acid and citric acid are the two main organic acids that are accumulated in the fruits and cause the sour taste. Citrus fruits contain more citric acid and other fruits like apples and pears contain malic acid. I will provide a detailed analysis for both cases.

In an article by Deshpande &amp; Ramakrishnan they mention two hypotheses about the accumulation of the citric acid in fruits. One was that the organic acids are not formed in the fruit, but transported to the fruit, in which the fruit acts like a storage organ. The other hypothesis is that the carbohydrates in the fruit is converted to the organic acid. In their research. they tried to isolate some enzymes from the Garcinia fruit to support the second hypothesis. They say:

This [reciprocal relation between acid and sugar content] would indicate the transformation of sugar to citric acid in the fruit tissue itself due to the presence of the requisite enzyme systems.
They suggest that the TCA cycle is how the citric acid is being accumulated and stored.

The article in the comments by Lobit et al. also supports a similar hypothesis of the accumulation of malic acid. The authors tell us that the malic acid is most likely produced in the fruit itself because the pH of the xylem and phloem (the transporting organs of plants) have a pH of close to 7 or higher. It seems that the malic acid is stored in the vacuoles of the mesocarp cells and that the amount of malic acid in the fruit depends on the transport of the malic acid to the vacuoles in the mesocarp cells.

Pathways

The actual pathways for the creation of citric acid and malic acid are very complex. Here is a picture of the different pathways in the cytosol of the mesocarp cells thst create these organic acids (picture taken from Etienne et al.) 

The first pathway described is the PEP pathway, which is really a modified CAM or C4 pathway (which are teo ways to deal with photorespiratin, and are adaptations to hot, dry environments). The PEP is converted to oxaloacetate by PEPC, which also takes in CO2, then that oxaloacetate is converted to malate by NAD-MDH. PEPC is regulated by malate concentration and cystolic pH, showing that the activity of PEPC is to indirectly increase the malate concentrations, not just to create more oxaloacetate.Let me note that I still don't fully understand why C4 and CAM-like pathways are present in Fruits from C3 plants.

The major pathway for citrate synthesis is the TCA cycle. Both are also involved in metabolism. TCA cycle (the citric acid cycle) is an important cycle and the second pathway in aerobic respiration. This pathway occurs in the mitochondria. Malate reacts with acetyl-CoA to make citrate and the CoA is released. The citrate then goes on to be repeatedly oxidized and ends up back to oxaloacetate. Now, for metabolic reasons, for example, if the amount of ATP created is top much at the end of aerobic respiration, the citrate could be transported out of the mitochondria, leading to citrate accumulation. 

Control of acid accumulation

The control of the organic acid accumulation is different for both malic acid and citric acid. Malic acid accumulation in the vacuoles is controlled by vacuolar storage and vacuolar transport (Etienne et al). This hypothesis was really the subject of the article in the comments. Their article was about modeling the malic acid based on this hypothesis and compare it to actual biochemical analysis of fruits. Their results supported the hypothesis.

The accumulation of citric acid is more dependent on metabolism, therefore respiration. This is since the vacuolar transport is slower for citrate than malate, and so whatever limits thr speed of malate vauolar transport affects citrate. But citrate concentration in thr cytosol would be the only other main factor affecting the vacuolar transport. A model by Wu et al. was created based on this hypothesis and compared to the actual citrate concentrations, and once again, this hypothesis also supported.

Functions of these organic acids

Suprisingly, the only functions of malate and citric acid cycle is mainly for respiration(mainly TCA cycle), and other metabolic processes. Malate does affect the texture, ripening, and starch metabolism and more of fruits, as described in this article. But, malate and citrate are important for all respiring organisms, including us.