Diabetics could soon be treated with personalised diets based on cereals and whole grains, say scientists.
Foods high in fibre boost gut bacteria that control blood sugar by breaking down carbohydrates.
It could lead to better therapies for type 2 diabetes which affects more than 3.5 million Britons bringing to an end their strict drugs regime.
It could also prevent the disease occurring. The number of diabetics is expected to rise to five million by 2025 because of the obesity crisis.
The study found dramatic improvements in those who followed the eating plan - in less than three months.
They also lost more weight and had healthier blood fat levels.
Lead author Professor Liping Zhao, a microbiologist at Rutgers University-New Brunswick, New Jersey, said: "Our study lays the foundation and opens the possibility that fibres targeting this group of gut bacteria could eventually become a major part of your diet and your treatment."
Type 2 diabetes can lead to a range of devastating problems from heart attacks and strokes to kidney failure, blindness and losing a limb.
But evidence is growing that one of the most widespread and deadly medical conditions of the modern age, which is linked to lifestyle factors like obesity, can be combated through diet.
Last year a study showed losing weight by following an 800-calorie-a-day eating plan for eight to 12 weeks can send the disease into remission.
The more weight participants lost, the more likely patients were to see an end to the condition.
Prof Zhao's pioneering high-fibre diet, which included whole grains and Chinese medicinal foods, followed participants for six years and found it rebalanced their gut bacteria.
This is the ecosystem in the gastrointestinal tract that help digest food and is vital for overall health. Fruit and vegetables are also rich in fibre.
Type 2 diabetes develops when the pancreas makes too little of glucose controlling insulin, or when the body doesn't use the hormone well.
Gut bacteria produce short-chain fatty acids that nourish intestine lining cells, reducing inflammation and helping to control appetite.
A shortage has been associated with type 2 diabetes and other diseases.
Previous research has found increasing dietary fibre consumption may relieve type 2 diabetes, but Prof Zhao and colleagues say they have finally unlocked the mechanism.
Working with colleagues in China, patients were selected at random to go on the high fibre diet, which also included prebiotics that boost short-chain fatty acids.
A second group acted as a control, receiving standard advice and food recommendations.
All 43 participants consumed similar amounts of energy and major nutrients, and took the blood glucose drug acarbose.
After 12 weeks, the average reduction in blood glucose levels was greater in the 27 on the high-fibre diet than the 16 control subjects.
Their fasting blood glucose levels also dropped faster, and they shed more pounds.
Surprisingly, of the 141 strains of short-chain fatty acid-producing gut bacteria identified by state of the art sequencing, only 15 are promoted by consuming more fibres.
These are likely to be the key drivers of better health, explained the researchers. Bolstered by the foods, they became the dominant strains in the gut after they increased levels of the short-chain fatty acids butyrate and acetate.
These acids created a mildly acidic gut environment that reduced populations of detrimental bacteria and led to increased insulin production and better blood glucose control.
The researchers said the study published in Science supports establishing a healthy gut microbiota as a new nutritional approach for preventing and managing type 2 diabetes.
Prof Zhao said it suggests type 2 diabetes may be caused by the loss of a beneficial function in gut bacteria.
He explained: "In ecological terms, the production of short-chain fatty-acids (SCFA) from carbohydrate fermentation, which is needed to maintain human health, can be considered an 'ecosystem service' provided by the gut microbiota to human hosts.
"Restoring or enhancing the lost or deficient function by reestablishing the functionally active ecological populations as ecosystem service providers (ESPs) is the key to a healthier microbiota, which can help alleviate disease.
"Targeted promotion of the active SCFA producers as ESPs via personalised nutrition may present a novel ecological approach for manipulating the gut microbiota to manage type 2 diabetes and potentially other related diseases."